将文本从一个文件复制到另一个文件

时间:2015-12-09 21:06:08

标签: c++

我尝试了两种不同的while循环。我认为他们有类似的问题。他们都没有终止或提供任何输出。

任务是将(逐字节)一个文件复制到另一个文件中。该文件不必具有最终行,也不必是.txt文件(可能是.exe ...)。

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std;


int main(int argc, char *argv[])
{
char c, c1, c2;
ifstream inFile;
ofstream outFile;

inFile.open("blackbird.txt");

if (inFile.fail())
{
    cout << "\nThe file was not successfully opened for reading."
    << "\nPlease check that the file currently exists.\n\n";
    exit(1);
}

cout << "\nThe file has been successfully opened for reading.\n\n";


outFile.open("blackbird_copy.txt");

if (outFile.fail())
{
    cout << "The file was not successfully opened for writing" << endl;
    exit(1);
}

cout << "The file has been successfully opened for writing.\n\n";


//outFile << "Hello";

// 1) this loop doesn't terminate. 2) the computer doesn't know what c1     is.
/*
while (inFile.get(c1))
{
    outFile << c1;
    cout << c1;
}
*/



// This one is no better
/*
while (inFile.good())
{
    inFile.get(c);
    outFile << c;
}
*/


inFile.close();
outFile.close();


// read contents of blackbird_copy to check our work.

inFile.open("blackbird_copy.txt");

while (inFile.get(c2))
{
    cout << c2;
}

cout << endl << endl;
return 0;
}

1 个答案:

答案 0 :(得分:0)

inFile.eof()读取一个字符并将其存储到c1(如果可用)。否则,保留ch未修改并设置failbit和eofbit。

您可以使用require("imports?this=>window!angular-localforage/dist/angular-localForage.js") 检查是否已到达文件末尾。

相关问题