2017-04-06 71 views
-1

我不知道爲什麼,但是當我做./a.out,它給我的錯誤:如果我評論的fseekftellFSEEK過程與退出代碼完成11

Process finished with exit code 11

,這不是」給我一個錯誤? 爲什麼? 我做了一個邏輯錯誤?

代碼:

#include <stdio.h> 
#include <stdlib.h> 


int main(){ 


     FILE *file_pointer; 
     int size=0; 


     if((file_pointer = fopen("file.txt","rb")) == NULL) 
     { 
      printf("Ok Man File was charge in the pointer"); 
      fseek(file_pointer, 0, SEEK_END); 
      size=ftell(file_pointer); 

      printf("%d",size); 
      rewind(file_pointer); 
      fclose(file_pointer); 
     } else 
      printf("File Not Found"); 




    return 0; 
} 

回答

1

你只是測試開放文件是否失敗,如果失敗,你去嘗試並使用它。 NULL的返回值表示一個錯誤,並且您不能使用句柄。

所以,你應該改變你的==!=

if((file_pointer = fopen("file.txt","rb")) != NULL) 
{ 
    ... 
} 

或稍多易讀的樣式(在我看來):

file_pointer = fopen("file.txt","rb"); 
if(file_pointer) 
{ 
    ... 
} 

順便說一句,有沒有必要rewind前你fclose

+0

它不是一個視頻,你正在返回商店,你不必倒帶成爲禮貌 – pm100

+0

謝謝你,祝你有美好的一天! –

相關問題