此代码应该读取用户指定的文件(我使用6行.txt),从原始文件的底部抓取一些行并将它们粘贴到新文件中。
问题是我似乎无法写入我的archSal.txt。我已经验证读取了原始文件,创建了archSal,成功输入了要写入的循环,并且同步了iCount和iIgnore。我做错了什么?
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void CopiaFichero (int iNum, int iCount, ifstream & archEntrada)
{
ofstream archSalida("archSal.txt");
string linea;
int iIgnora = (iCount - iNum) +1;
archEntrada.clear();
archEntrada.seekg(0, ios::beg);
iCount = 0;
archSalida.open("archSal.txt");
// write loop
while (getline(archEntrada, linea))
{
if (iIgnora == iCount)
{
archSalida << linea << endl;
iIgnora++;
}
iCount++;
}
archSalida.close();
}
int main()
{
int iNum = 0, iCount = 0;
bool hayLineas = false;
string nomArch, linea;
ifstream archEntrada(nomArch);
cout << "Type in the name of the file" << endl;
cin >> nomArch;
cout << "Type in the number of lines to copy " << endl;
cin >> iNum;
archEntrada.open(nomArch);
// verify that lines to copy is less than or equal to actual lines
do
{
while(getline(archEntrada, linea))
{
++iCount;
}
if (iCount >= iNum)
{
hayLineas = true;
}
else
{
cout << "A copy cannot be made" << endl;
cout << "Type in the number of lines to copy " << endl;
cin >> iNum;
}
}while (hayLineas != true);
// Call function with data
CopiaFichero(iNum, iCount, archEntrada);
archEntrada.close();
return 0;
}