您的位置:首页 > 其它

在一个文件末尾增加字符串,并在控制台打印出来

2015-09-01 20:58 218 查看
#include<stdio.h>
#include<stdlib.h>

int main()
{
FILE *fp_write, *fp_read;
char ch;
char add[] = "An extra line\n";

fopen_s(&fp_write,"E:\\first.txt","a+");
if(fp_write == NULL)
{
printf("Can't open the file!\n");
system("pause");
return 0;
}
//定位到文件末尾
fseek(fp_write,0,SEEK_END);
fputs(add,fp_write);
//fwrite(add,sizeof(add),1,fp_write);
//先关闭写入流再打开读出流
fclose(fp_write);

fopen_s(&fp_read,"E:\\first.txt","r");
if(fp_read == NULL)
{
printf("fp_read,Can't open the file!\n");
system("pause");
return 0;
}
while((ch = fgetc(fp_read)) != EOF)
putchar(ch);

fclose(fp_read);

system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: