這就是我想要做的,未處理的異常,將信息寫入文件C語言
我有一個帶有信息的文件。我試圖在每行之前重寫它,行號會出現。
的基本想法,我心目中是這樣工作的:
while i haven't reached the end of the file:
save the first line of the file (100 characters or until null is reached) in str
go back to the file, and write "line number" and then the info in str.
now str takes the next 100 chars...rinse and repeat.
實際代碼:
void add_line_number(FILE* f1)
{
char str[100];
int i=1;
fseek(f1,0,SEEK_SET);
do
{
fgets(str,100,f1);
fprintf(f1,"%d %s",i,str);
i++;
f1+=strlen(str);
}while(strlen(str));
}
得到的錯誤:未處理的異常在0x77e78dc9徵文4.exe:0000005:訪問衝突寫位置0xfffff204。
我試過了。我進入一個無限循環,因爲海峽始終以相同的字符串(從f1到下一個\ 0,因爲F1是恆定的,所以是STR) – 2013-05-13 19:59:04
此外,在同一圈上的同一個文件*做fprintf中作爲與fgets是不是一個好主意。 – edtheprogrammerguy 2013-05-13 20:09:14