【发布时间】:2015-08-09 19:36:34
【问题描述】:
C - fprintf 没有写入文件,知道为什么吗?
#include <stdio.h>
#include <stdlib.h>
int main(void){
FILE* pfile=fopen("/home/user-vlad/Programming/C-other/meme.txt","r");
if(pfile==NULL){
printf("ERROR: Stream is equal to NULL\n");
exit(1);
}
fprintf(pfile,"Hello");
fclose(pfile);
return 0;
}
编译器:clang,操作系统:FreeBSD
【问题讨论】:
-
您打开文件只是为了阅读。阅读manual page for
fopen以及第二个参数的含义。"r"表示您只想阅读。有写入、追加等选项。 -
当 fopen 错误发生时(pfile 为 NULL)然后使用 'perror()'
因为错误的系统消息将附加到您在括号之间放置了双引号。 IE。 perror("fopen fo meme.txt 失败");