我寫了下面的代碼:文件I/O fopen()函數返回NULL
void WriteToFile(const char** strings, const char* path, int n)
{
FILE* fp = fopen(path, "w");
int i;
if(fp)
{
for(i = 0 ; i < n ; i++)
{
puts(strings[i]);
fprintf(fp, "%s\n", strings[i]);
}
}
else
{
printf("Error at writing to file.\n");
exit(1);
}
fclose(fp);
}
我得到一個錯誤 - 過於意味着fopen()
沒有工作,怪異,我打印的路徑 - FP指向NULL它沒有\n
或其中的奇怪的東西,它可以在我的電腦中。
你對文件有寫權限嗎?使用調試器,複製路徑並將其粘貼到記事本的「打開」對話框中,查看記事本是否可以找到複製粘貼的路徑。 –
[fopen set errno upon failure](http://linux.die.net/man/3/fopen),失敗後errno設置了什麼?使用[strerror](http://linux.die.net/man/3/strerror)作爲錯誤字符串的文本版本。 – fvu
你是否試圖無權限寫入文件夾? – BLUEPIXY