gpt4 book ai didi

c - 文件指针 "outdated?"

转载 作者:行者123 更新时间:2023-11-30 20:57:02 26 4
gpt4 key购买 nike

嗨,我已经开始学习一些有关文件指针以及如何打开文件等的知识。我正在阅读 Stephen Prata 所著的《C Primer Plus 第五版》(SamsPublishing)我什至无法获得他们在我的项目中工作所需的解决方案。

这就是它的样子

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
int byte;
FILE * source;
int filect;


if (argc == 1)
{
printf("Usage: %s filename[s]\n", argv[0]);
exit(EXIT_FAILURE);
}

for (filect = 1; filect < argc; filect++)
{
if ((source = fopen(argv[filect], "r")) == NULL)
{
printf("Could not open file %s for input\n", argv[filect]);
continue;
}
while ((byte = getc(source)) != EOF)
{
putchar(byte);
}
if (fclose(source) != 0)
printf("Could not close file %s\n", argv[1]);
}
return 0;
}`

输出是:用法:(我的c项目所在的位置)文件名[s]按任意键继续...为什么会发生这种情况?

最佳答案

argv[0] 可以引用运行程序时可执行文件所在的目录。该标准规定如下:

If the value of argc is greater than zero, the string pointed to by argv[0] represents the program name; argv[0][0] shall be the null character if the program name is not available from the host environment.

通过命令行传递给应用程序的参数可以从 argv[1] 开始一直到 argv[argc - 1] 进行访问。

关于c - 文件指针 "outdated?",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16741748/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com