以只写的方式打开一个文件a.txt,并将字符串“hello world”写入到该文件内。

用c语言,以只写的方式打开一个文件a.txt,并将字符串“hello world”写入到该文件内。

再用以只读的方式打开文件a.txt,并将该文件中的内容读出并打印在显示器上。

#include <stdio.h>

int main()
{
FILE *fp = NULL;
char str[50] = {0};
fp = fopen("c:\\a.txt","w");
if(fp==NULL)
{
printf("file write error!\r\n");
return -1;
}
fputs("hello world!\n",fp);
fclose(fp);
fp = NULL;
fp = fopen("c:\\a.txt","r");
if(fp==NULL)
{
printf("file read error!\r\n");
return -2;
}
fgets(str,sizeof(str),fp);
fclose(fp);
printf("%s",str);
return 0;
}






温馨提示:答案为网友推荐,仅供参考
相似回答