2013-02-07 30 views
1

返回這個簡單的程序,讓我煩惱與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; 
} 
+2

什麼是錯誤號設置爲?根據fputs()文檔,當EOF返回時,值errno將被設置爲一個指示特定的代碼。 –

+1

...和'perror'會爲你打印錯誤。 –

回答

4

您正在打開文件以供閱讀,但正在嘗試向其中寫入數據?這沒有意義。

1

在'openFile()'中,您使用'r'打開文件,但'fputs'想要'w'該文件。

1

Hm:& stringVector [0]?

這和做的完全一樣:a = 1 - 1,爲什麼不做a = 0?

- > stringVector = & stringVector [0]

+0

這是我的小白! –