2014-03-13 24 views
0

我一直在用c編寫一個小程序,正在創建的文件是正確的,直到最後150行左右,它開始獲取隨機字符,有時與我的程序的文件路徑或它的一部分寫入它。爲什麼文本寫入我的文件完美的罰款,直到最後幾行,當它在c循環?

注意:我使用mac視網膜和Xcode編寫和測試。

編輯:除非你想看到整個功能我已經把所給我的麻煩下面這段代碼

int vieditor() 
{ 
    FILE *readtext, *writetext,*testfile,*writeagain; 
    char mytest[1000][81],controller = '\0',worq;; 
    int x,charactersperline = 0,lines = 0,i,k; 



    /* this just creates a file with 1000 lines */ 
    testfile = fopen("/Users/scottcarlson/Classes/CS_222/test.txt","w"); 
    for (i = 0; i<1010; i = i+1) 
    { 
    fprintf(testfile,"this is line %d \n",i); 
    } 
    fclose(testfile); 
    /* end of file creation */ 

    readtext = fopen("/Users/scottcarlson/Classes/CS_222/test.txt","r"); 
    writetext = fopen("/Users/scottcarlson/Classes/CS_222/test1.txt","w"); 

    if (!readtext) 
    { 
     printf ("test.txt does not exist"); 
    } 
    else 
    { 
     while ((x = fgetc(readtext)) != EOF) 
     { 
      if (charactersperline < 80 && lines < 1000) 
      { 
       fprintf(writetext,"%c",x); 
       char ch = x; 
       mytest[lines][charactersperline] = ch; 
       if (lines < 20) 
       { 
        printf("%s",&mytest[lines][charactersperline]); 
       } 
      } 

      if (x != 10) 
      { 
       charactersperline += 1; 
      } 
      else 
      { 
       charactersperline = 0; 
       lines += 1; 
      } 

     } 
     fclose(readtext); 
     fclose(writetext); 


    } 
    do 
    { 
     fseek(stdin,0,SEEK_END); 
     system("stty raw -echo"); 
     controller = getchar(); 
     system("stty cooked echo"); 
    } 
    while (controller != 58); 
    scanf("%s",&worq); 
    if (worq == 'w') 
    { 
      writeagain = fopen("/Users/scottcarlson/Classes/CS_222/test.txt","w"); 
      for (k = 0; k<lines; k = k+1) 
      { 
        fprintf(writeagain,"%s",mytest[k]); 

      } 

    } 
    else if (strncmp(&worq,"q!",5)) 
    { 
     return 0; 
    } 


    return 0; 
} 

這是整個功能的一部分,但你並不需要看那個除非你想,那是害我麻煩的部分是底部就在這裏

do 
{ 
    fseek(stdin,0,SEEK_END); 
    system("stty raw -echo"); 
    controller = getchar(); 
    system("stty cooked echo"); 
} 
while (controller != 58); 
scanf("%s",&worq); 
if (worq == 'w') 
{ 
     writeagain = fopen("/Users/scottcarlson/Classes/CS_222/test.txt","w"); 
     for (k = 0; k<lines; k = k+1) 
     { 
       fprintf(writeagain,"%s",mytest[k]); 

     } 

} 
else if (strncmp(&worq,"q!",5)) 
{ 
    return 0; 
} 


return 0 

這應該打印(覆蓋)文件test.txt,其中它完美的罰款高達約840行,據我所知,根本沒有意義。

如果任何人都可以提供幫助,將不勝感激,因爲我無法在其他任何地方找到這方面的信息。

這裏是test.txt文件的一部分,它開始寫隨機事物。

this is line 838 
this is line 839 
∆ø_ˇthis is line 840 
¿_ˇthis is line 841 
this is line 842 
ˇthis is line 843 
this is line 844 
this is line 845 
this is line 846 
this is line 847 
’ø_ˇthis is line 848 
–è7zthis is line 849 
_ˇthis is line 850 
ˇthis is line 851 
this is line 852 
this is line 853 
this is line 854 
Ä◊ø_ˇthis is line 855 
◊ø_ˇthis is line 856 
ïˇthis is line 857 
_ˇthis is line 858 
this is line 859 
this is line 860 
this is line 861 
this is line 862 
∞Ãø_ˇthis is line 863 
Ãø_ˇthis is line 864 
éìˇthis is line 865 
this is line 866 
ˇthis is line 867 
/lithis is line 868 
this is line 869 
this is line 870 
p¶√_ˇthis is line 871 
de/DerivedData/Project_5.c-akilnssybkcyvuetuwkrqlyxxtie/Build/Pthis is line 872 
ystem_pthread.dylibthis is line 872 
ystem_pthread.dylibthis is line 873 
this is line 874 
ˇthis is line 875 
this is line 876 
this is line 877 
this is line 878 
this is line 879 
.c-akilnssythis is line 880 
this is line 881 
_ˇthis is line 882 

回答

0

你不是null-terminating你的行mytest。當使用%s打印時,fprintf希望在字符串末尾看到'\ 0'。

將字符逐個字符複製到mytest中時,您需要將代碼添加到每行末尾的'\ 0',或者您可以在開始時將整個數組初始化爲'\ 0'。 最簡單(但並非最有效)的方法是在每次添加字符後添加'\ 0'。

mytest[lines][charactersperline] = ch; 
mytest[lines][charactersperline+1] = '\0'; 

您還可以在該行的開始,當你開始一個新行添加一個零字節,因爲你的循環可能不會繼續圍繞把任何東西到該行。

注意,也有一個問題,如果你沒有在文件中有什麼,或者如果你的文件不以「\ n」結束,因爲你不會增加行數

而且

printf("%s",&mytest[lines][charactersperline]); 

應該%C而不是%S作爲要打印一個字符。

+0

它工作!非常感謝。如果沒有太多的麻煩,你或某人可以解釋爲什麼該程序將不同文件路徑的部分放在我的打印文本文件中?我得到它在最後需要'\ 0',但我不明白爲什麼它會完全導致這些類型的錯誤。再次感謝你是救星! – scarlso9

+0

mytest正在使用的內存沒有初始化,這意味着它有任何隨機的東西發生在內存中開始。如果沒有字符串末尾的'\ 0',printf將繼續打印所有額外的東西,直到它碰到另一個'\ 0'字節。打印的文件路徑可能被程序的另一部分(例如運行時庫啓動)使用,並將內存返回給程序使用。 –

0
scanf("%s",&worq); 

它看起來像worqchar"%s"轉換說明符將匹配字符串,並在提供的地址(單個字符的地址)中寫入字符串中的所有字符。

我懷疑你有緩衝區溢出的問題(即使只讀了1個字符,scanf將在無效位置寫入終止零字節)。

您可以使用%c轉換說明符讀取單個字符。

+0

我改變了這個,上面的帖子解決了大部分問題,但是在我的文件結束後我仍然得到一個奇怪的讀數,它應該停止打印,並且這個問題解決了! – scarlso9

相關問題