C 简单的fputs()示例返回EOF
这个简单的程序在fgets中给我带来麻烦,返回EOF,这是fgets的一个错误值 我不明白问题出在哪里C 简单的fputs()示例返回EOF,c,fputs,C,Fputs,这个简单的程序在fgets中给我带来麻烦,返回EOF,这是fgets的一个错误值 我不明白问题出在哪里 #include <stdio.h> #include <stdlib.h> #include <unistd.h> FILE* openFile(const char* path) { FILE* file; file = fopen(path, "r"); if(file == NULL) { perro
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
FILE* openFile(const char* path)
{
FILE* file;
file = fopen(path, "r");
if(file == NULL)
{
perror(path);
exit(EXIT_FAILURE);
}
return file;
}
int main()
{
FILE* file;
char stringVector[6] = "hello";
file = openFile("/home/user/workspace/fputs/src/testo.txt");
if(fputs(&stringVector[0], file) == EOF)
{
printf("error in fputs");
fclose(file);
exit(EXIT_FAILURE);
}
fclose(file);
return 0;
}
您正在打开文件进行读取,但仍在尝试向其写入数据?这是没有道理的。您正在打开文件进行读取,但却试图向其中写入数据?这没有意义。在“openFile”中,您用“r”打开文件,但“fputs”想要“w”文件。在“openFile”中,您用“r”打开文件,但“fputs”想要“w”文件。Hm:&stringVector[0] 这和做a=1-1完全一样,为什么不做a=0呢 ->stringVector=&stringVector[0]Hm:&stringVector[0] 这和做a=1-1完全一样,为什么不做a=0呢
->stringVector=&stringVector[0]错误号设置为什么?根据fputs文档,当返回EOF时,值errno将被设置为指示具体情况的代码。。。。perror将为您打印错误。errno设置为什么?根据fputs文档,当返回EOF时,值errno将被设置为指示具体情况的代码。。。。perror将为您打印错误。