【发布时间】:2018-06-27 00:34:12
【问题描述】:
我正在尝试用 C 语言编写一个程序,该程序将逐个字符地遍历文件并将它们打印到控制台。但是,在运行下面的代码时,我收到错误Segmentation fault: 11。我的代码中的问题在哪里?
void readFile(char fileName);
int main(const int argc, char *argv[])
{
int status = 0;
for (int i = 1; i < argc; i++) {
readFile(*argv[i]);
}
exit(status);
}
void readFile(char fileName)
{
FILE* file;
char c;
file = fopen(&fileName, "r");
do {
c = getc(file);
printf("%c", c);
if (!isalpha(c)) {
printf("\n");
}
} while (c != EOF);
fclose(file);
}
【问题讨论】:
-
调试器会告诉你哪一行有段错误。
-
你的
readFile逻辑错误:你在检查是否为EOF之前使用c,而c的类型错误(必须是int)。
标签: c file command-line segmentation-fault