C语言中,读取一个TXT文件,然后对其内容进行更改,如何将更改的内容进行保存
#include<iostream>usingnamespacestd;voidmain(){FILE*fp;charch;charchh;charg[600];inti...
#include <iostream>
using namespace std;
void main()
{
FILE * fp; char ch;
char chh;
char g[600];
int i=0;
if((fp=fopen("3.txt","r"))==NULL)
{
cout << "Error.";
exit(0);
}
while((ch=fgetc(fp))!=EOF)
{
g[i]=ch;
if(i%1==0&&i!=0)
cout<<",";
if(i%8==0)
cout<<endl;
cout << g[i];
i++;
}
fclose(fp);
system("pause");
} 展开
using namespace std;
void main()
{
FILE * fp; char ch;
char chh;
char g[600];
int i=0;
if((fp=fopen("3.txt","r"))==NULL)
{
cout << "Error.";
exit(0);
}
while((ch=fgetc(fp))!=EOF)
{
g[i]=ch;
if(i%1==0&&i!=0)
cout<<",";
if(i%8==0)
cout<<endl;
cout << g[i];
i++;
}
fclose(fp);
system("pause");
} 展开
2个回答
展开全部
把cout重定向到文件:
------------------------------------------------------
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream outfile("out.txt");
streambuf *streams=cout.rdbuf();//保存原来的cout对象
cout.rdbuf(outfile.rdbuf());
cout<<"yanghaijunlovechenchen!sssssssssssssssssssssssssssssssssssss"<<endl;
cout<<"yanghaijunlovechenchen!sssssssssssssssssssssssssssssssssssss"<<endl;
cout<<"yanghaijunlovechenchen!sssssssssssssssssssssssssssssssssssss"<<endl;
cout<<"yanghaijunlovechenchen!sssssssssssssssssssssssssssssssssssss"<<endl;
cout.rdbuf(streams);//恢复对象 保存和恢复必须成对出现不然会出现错误
cout<<"yanghaijunlovechenchen!"<<endl;
return 0;
}
展开全部
#include <stdio.h>
int main(void)
{
FILE *fp;
FILE *fp2;
char ch;
char chh;
char g[600];
int i=0;
if((fp=fopen("3.txt","r"))==NULL)
{
printf("Error.");
return (0);
}
if((fp2=fopen("4.txt","w"))==NULL)
{
printf("Error 2.");
return (0);
}
while((ch=fgetc(fp))!=EOF)
{
g[i]=ch;
if(i%1==0&&i!=0)
fputc(',', fp2);
if(i%8==0)
{
fputc('\n', fp2);
}
fputc(g[i], fp2);;
i++;
}
fclose(fp);
fclose(fp2);
printf("Done\n");
getchar();
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询