返回這個簡單的程序,讓我煩惱與fgets(),返回EOF,這是與fgets() 的誤差值,我不明白問題出在哪裏只需FPUTS()的例子EOF
#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;
}
什麼是錯誤號設置爲?根據fputs()文檔,當EOF返回時,值errno將被設置爲一個指示特定的代碼。 –
...和'perror'會爲你打印錯誤。 –