C++ 文件输入输出题

来源:百度知道 编辑:UC知道 时间:2024/06/11 14:48:37
求下题代码!
打开一个已经存在的TXT文件,在该文件的每一行内容前面加上一个行号,保存在另外一个文本文件中。用流式IO实现!
已经存在的TXT文件中有若干行内容

#include <fstream>
using namespace std;
void DoIt()
{
ifstream fileIn;
ofstream fileOut;
char ch;
int index=0;

fileIn.open("testIn.txt");
fileOut.open("testOut.txt");

if(!fileIn.eof())
{
index++;
fileOut << index << " ";
}
while(!fileIn.eof())
{
ch=fileIn.get();
fileOut << ch;
if(ch=='\n')
{
index++;
fileOut << index << " ";
}
}
}
void main()
{
DoIt();
}

试下吧先~~

#include <fstream>
using namespace std;
int main()
{
ifstream in;
in.open("1.txt");
if(!in.good())
exit(0);
ofstream out;
out.open("2.txt");
int count = 0;
string str;
while(getline(in,str))
{