gpt4 book ai didi

C 错误(初级)

转载 作者:太空狗 更新时间:2023-10-29 15:44:22 24 4
gpt4 key购买 nike

#include <stdio.h>

main(void) {
file *file = fopen("words.txt","r")
if(file != null) {
char line[128];
while(fgets( line, sizeof line, file) != null)
{
fputs ( line, stdout );
}
fclose ( file );
}
}

这是我的代码。我正在尝试读取文件并输出内容。但这给了我错误代码

main.c: In function 'main':
main.c:4: error: 'file' undeclared (first use in this function)
main.c:4: error: (Each undeclared identifier is reported only once
main.c:4: error: for each function it appears in.)
main.c:5: error: parse error before "if"
main.c:7: error: 'line' undeclared (first use in this function)
main.c:7: error: 'null' undeclared (first use in this function)
main.c: At top level:
main.c:13: error: parse error before '}' token
main.c:13:2 warning: no newline at end of file

我该如何解决这个错误。

最佳答案

FILE和NULL写错了(C区分大小写)。fopen 行缺少一个分号。

下面的代码编译并运行。

#include <stdio.h>

main(void) {
FILE *file = fopen("words.txt","r");
if(file != NULL) {
char line[128];
while(fgets( line, sizeof line, file) != NULL)
{
fputs ( line, stdout );
}
fclose ( file );
}
}

关于C 错误(初级),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/609447/

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