C++怎么读取TXT文本中的信息和写入信息到TXT文本中去

读1.TXT文本中的信息并把字符串”123”写入到2.TXT中去,要完整并能运行的代码,谢谢啦

第1个回答  推荐于2016-06-15
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void main()
{
//读取1.txt全部内容
ifstream ifs("1.txt");//如果需要,请修改文件路径
if(!ifs)
{
cout<<"文件打开失败!";
return;
}
char ch[100];
while (!ifs.eof())
{
ifs.getline(ch,100);
cout<<ch<<endl;
}
//字符串“123”写入2.txt
char a[]="123";
ofstream ofs("2.txt");
ofs<<a;
}本回答被提问者采纳
第2个回答  2019-10-17
#include
<iostream>
#include
<fstream>
#include
<string>
using
namespace
std;
void
main()
{
//读取1.txt全部内容
ifstream
ifs("1.txt");//如果需要,请修改文件路径
if(!ifs)
{
cout<<"文件打开失败!";
return;
}
char
ch[100];
while
(!ifs.eof())
{
ifs.getline(ch,100);
cout<<ch<<endl;
}
//字符串“123”写入2.txt
char
a[]="123";
ofstream
ofs("2.txt");
ofs<<a;
}
第3个回答  2020-04-13
#include
#include
#include
using
namespace
std;
void
main()
{
//读取1.txt全部内容
ifstream
ifs("1.txt");//如果需要,请修改文件路径
if(!ifs)
{
cout<<"文件打开失败!";
return;
}
char
ch[100];
while
(!ifs.eof())
{
ifs.getline(ch,100);
cout<
评论
0
0
加载更多
第4个回答  2010-05-30
通过Istream 和Ostream 进行读写操作
相似回答