我创建了一个程序,它在屏幕上依次显示命令行中列出的所有文件的内容。
但是,当我在终端中运行它时,我实际上无法打开任何我尝试“提供”它的文件。
有谁知道我怎么能让它发挥作用?
以下是我在Mac上输入终端的示例:
"John_Smith-MacBook:Desktop smith_j$ "/Users/smith_j/Desktop/Question 3-28-13 5.10 PM/usr/local/bin/Question" helloworld.txt
Could not open file helloworld.txt for input"
这是我使用过终端的第一天,如果答案非常简单,请原谅我。
这是我的代码:
#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;
}
答案 0 :(得分:0)
查看errno
[可能与perror()
]的值,以便了解无法打开的原因。
简单示例:
perror("fopen failed:");
printf("errno = %d.\n", errno);
它将使用“fopen Failed:”为errno条件(库提供)的文本版本添加前缀,然后给出特定的errno值。
上面命令行的文本看起来很可疑,请检查它是否正确。更好的是,尝试使用更简单的命令行调用您的程序,例如,使用您的二进制文件在当前目录中的一个文件。
将二进制程序和数据文件放在同一目录中,而不是所有长路径名。然后,从该位置开始./myprog filename.txt
这将减少拼写错误执行的可能性。