c++一个读取写入txt文件的实现解决方案

c++一个读取写入txt文件的实现
#include <fstream>
#include <cstdlib>
#include <iostream>
using namespace std;

#define inFile "inData.txt"
#define outFile "outData.txt"

//Function used...
//Copies one line of text 
int copyLine(ifstream&,ofstream&);

int main()
{
int lineCount;
ifstream ins;
ofstream outs;
ins.open(inFile);
if (ins.fail())
{
cerr<<"** ERROR: Cannot open "<<inFile
<<" for input. **"<<endl;
return EXIT_FAILURE;
}
outs.open(outFile);
if (ins.fail())
{
cerr<<"** ERROR: Cannot open "<<outFile
<<" for output. **"<<endl;
return EXIT_FAILURE;
}
lineCount = 0;
while (copyLine(ins,outs) != 0)
{
lineCount++;
}
cout<<"Input file copied to output file."<<endl
<<lineCount++<<" lines copied.";
ins.close();
outs.close();
return 0;
}

int copyLine(ifstream& ins,ofstream& outs)
{
const char NWLN = '\n';
char nextChar;
int charCount = 0;
  ins.get(nextChar);
  while ((nextChar != NWLN) && !ins.eof())
  {
outs.put(nextChar);
charCount++;
ins.get(nextChar);
  }
while (!ins.eof())
{
outs.put(NWLN);
charCount++;
}
return charCount;
}


inData.txt文件里随便写了些东西,内容如下:

  你不知道的事
  --王力宏
  蝴蝶眨几次眼睛,才学会飞行。
  夜空散漫了星星,但几颗会落地。

就这样而已,运行后便没有反应了,outData.txt文件便会无限变大,关闭程序,outData.txt文件也打不开。

------解决方案--------------------
copyLine 函数有问题。
第一个 while 的判定条件是 遇到换行 或者 文件结束 就跳出;
然而第二个 while 循环是一直写入文件,但是并没有操作读取的文件,而且判定的条件是读取的文件没有结束;
如果第一个 while 遇到的是换行符跳出的循环,第二个循环就永远结束不了了……
------解决方案--------------------
第二个while有问题,没有对流进行任何操作,如果第一个while没有到流结尾,则第二个循环为死循环
只需要改成
while (!ins.eof())
{
outs.put(NWLN);
charCount++;
ins.get(nextChar);
}