2011-06-06 160 views
0

我有一個'C'程序遇到了一個奇怪的問題..我在包含「feof(fp)」的行中出現段錯誤.. 我是試圖在Linux上運行..幫助需要關於文件指針處的段錯誤

我甚至用gdb命令回溯程序.. 但它是沒有用的..

檢查我的樣本代碼..

char buf[2000],str[15],lno[5],def[15],ref[15],tmp[15],ch,ifile[20],ofile[20]; 

int i,j,oldi,count,c,r,d,f,t,lc=0; 



FILE *fp=NULL,*fpo=NULL; 

void xyzstart() 
{ 
/* 
*Some operation that is not at all concerned with the file 
* 
*/ 
} 

int main() 

{ 

printf("Enter the name of the input file\n"); 

gets(ifile); 



fp=fopen(ifile,"r"); 
if(fp==NULL) 

{ 

printf("Error"); 

exit(0); 

} 



printf("Enter the name of the output file\n"); 
gets(ofile); 

fpo=fopen(ofile,"w"); 


if(fpo==NULL) 

{ 

printf("Output file couldn't be opened\n"); 

exit(0); 

} 




while(!feof(fp)) 

{ 

fgets(buf,sizeof(buf),fp); 

count++; //Count the number of lines in a file 

} 



rewind(fp); //move the file pointer to the beginning of the file 



while(!feof(fp)) //Error is here!! Segmentation fault (Core Dumped)!! 

{ 

clear(); //User defined function which clears all the memory 

if(count==lc) 

{ 

nodef(); //User defined function which doesn't reads from or writes into a file 

noref(); //User defined function which doesn't reads from or writes into a file 

print(); //User defined function which writes the values to output file 

break; 

} 



fgets(buf,sizeof(buf),fp); 

{ 

i=0; 

lc++; 


    while(buf[i]!=' ') //read until it encounters a space.. 

     { 

     lno[i]=buf[i]; 

     i++; 

     } 

    lno[i]='\0'; 

//puts(lno);/// 

    } 



i++; 

oldi=i; 

ch=buf[i]; 

switch(ch) 

{ 

case 'x': xyzstart(); break; 

default: printf("Nothing found"); 
} 

} 



fclose(fpo); 

fclose(fp); 

return 0; 

} 

我真的不知道該怎麼做!任何人都可以請幫我嗎? 在此先感謝!

下面是既清晰又xyzstart()的代碼 無效明確()

{ 

memset(buf,'\0',sizeof(buf)); 

memset(lno,'\0',sizeof(lno)); 

memset(def,'\0',sizeof(def)); 

memset(ref,'\0',sizeof(ref)); 

i=oldi=0; 

memset(str,'\0',sizeof(str)); 

} 

void xyzstart() 

{ 

r=d=c=0; 

for(;;c++,i++) 

     { 

      if(buf[i]==' ') 

      break; 

      if(buf[i]=='(') break; 

      if(buf[i]==';')break; 

      if(buf[i]=='\n') break; 

      if(buf[i]=='=') break; 

      if(buf[i]=='+' || buf[i]=='-') break; 

      str[c]=buf[i]; 

     } 

     str[c]='\0'; 

if(buf[i]=='=') 

      assignment(); 

else if(buf[i]=='+' || buf[i]=='-') //Increments or decrements 

      incdec(); 

      else if(buf[i]=='(') 

       udefined(); 

} 
+0

你確定你的文件名少於19個字符嗎?如果他們不是,你會得到一個緩衝區溢出。 – 2011-06-06 17:13:32

+0

你正在使用'feof'錯誤。'feof'函數不會檢測下一次讀取是否會失敗,因爲文件位於最後;它測試最後的失敗是否是由於文件處於末尾。而且你使用它沒有失敗 – pmg 2011-06-06 17:41:36

回答

2

一些注意事項:

  1. 不要使用feof(fp)作爲while循環的條件;該函數只會在您嘗試讀取文件末尾後纔會返回true,因此您會經常結束循環。檢查您輸入操作的結果(fgets將返回失敗NULL)和然後測試EOF,像這樣:

    
    while(fgets(buf, sizeof buf, fp) != NULL) 
        count++; 
    if (feof(fp)) 
        printf("At end of file\n"); 
    

  2. 永遠永遠永遠永遠永遠永遠使用gets:它介紹點程序中出現故障(可能在這種情況下)。從C99開始,它已經被棄用,預計將從下一版本的語言中消失(是的,由這一個庫調用引起的混亂比打破30年以上遺留代碼的前景更加可怕)。請使用fgets或其他替代方法。

從您發佈的代碼,我沒有看到明顯問題;我不知道爲什麼feof將核心轉儲,如果前面的語句是成功rewind。我唯一可以確定的是文件指針被覆蓋了某處(可能通過調用gets緩衝區溢出)。

+1

在[C201x標準的當前草案]中刪除'gets()'(http://www.open-std.org/JTC1/sc22/wg14/www/docs/n1548。 PDF)。 – pmg 2011-06-06 17:48:52

+0

嘿謝謝你的回覆!我正在修改代碼。我會在幾分鐘之內讓你知道結果。 – abk07 2011-06-06 18:07:41

+0

嘿我對我的代碼做了以下更改.. 1)用scanf讀取文件名 2)按照John的建議檢查了feof。 3)用fseek代替倒片 4)按照John的建議使用fgets!它的工作!謝謝!! :) – abk07 2011-06-07 17:47:35

3

我的猜測是,堪稱while的功能之一是搗毀文件指針(可能關閉它)。

主要犯罪嫌疑人:clearxyzstart

+0

+1:的確如此。調試它的方法是使用Valgrind之類的東西,或者從循環中刪除東西,直到它不再發生seg-fault。 – 2011-06-06 17:14:58

+0

嘿,沒有..其實輸入文件無處可用! – abk07 2011-06-06 17:15:01

+0

「清除所有內存」聽起來像是一個壞的事情,一個功能做... – geoffspear 2011-06-06 17:15:38

1
  1. 究竟「清楚」幹什麼?
  2. 這個循環看起來很危險:

    而(BUF [I] =」「!)//讀取直到遇到一個空格..

如果BUF不包含空格?

+0

+1我認爲這是真正的問題:) – pmg 2011-06-06 17:39:21