我在嘗試使用fscanf從文件讀入字符串時遇到分段錯誤錯誤,任何幫助將不勝感激。fscanf分割錯誤 - C
int main()
{
char temp[100];
FILE *fp = fopen("test.txt", "r");
if (fp == NULL)
{
printf("error");
}
memset(temp, 0, strlen(temp));
while (fscanf(fp,"%s", temp)==1)
{
}
return 0;
}
輸入文件的內容是什麼? (它可能有超過99個字符的字符串嗎?) – davmac
除了其他的評論和回答:'memset(temp,0,strlen(temp));'當你'fscan'變成'temp'時並不需要,無論如何。 –
@MichaelWalz爲了安全起見,我仍然認爲在'char'數組的開頭至少有一個空終止符是一個好主意。 –