我一直在用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',但我不明白爲什麼它會完全導致這些類型的錯誤。再次感謝你是救星! – scarlso9
mytest正在使用的內存沒有初始化,這意味着它有任何隨機的東西發生在內存中開始。如果沒有字符串末尾的'\ 0',printf將繼續打印所有額外的東西,直到它碰到另一個'\ 0'字節。打印的文件路徑可能被程序的另一部分(例如運行時庫啓動)使用,並將內存返回給程序使用。 –