简体   繁体   English

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

[英]copy text from one file to another

I have tried two different while loops. 我尝试了两个不同的while循环。 I think they have a similar problem. 我认为他们也有类似的问题。 Neither of them terminate or give any output. 它们都不会终止或提供任何输出。

The task is to copy (byte for byte) one file into another. 任务是将一个文件(一个字节一个字节)复制到另一个文件中。 The file does not have to have endlines, nor does it have to be a .txt file (it could be .exe...). 该文件不必具有结尾,也不必是.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;
}

inFile.get(c1) Reads one character and stores it to c1 if available. inFile.get(c1)读取一个字符并将其存储到c1(如果有)。 Otherwise, leaves ch unmodified and sets failbit and eofbit. 否则,保持ch不变,并设置failbit和eofbit。

You can use inFile.eof() to check whether the end of the file has been reached. 您可以使用inFile.eof()来检查是否已到达文件末尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将文本从一个文件复制到另一个文件,然后将文本字符串的第一个字母转换为大写 - How to copy text from one file to another and then turning the first letters of the text string into uppercase Qt - 将文件从一个目录复制到另一个目录 - Qt - copy a file from one directory to another 如何将数字从一个文本文件复制到另一个文本文件但使它们成为下一个数字? - How do I copy numbers from one text file to another but make them the next number? 处理从一个文本文件到另一文本文件的行 - Processing lines from one text file to another LLVM IR:将函数从一个LLVM IR文件复制到另一个 - LLVM IR: Copy Function from one LLVM IR file to another 在 C++ 中将文件从一个目录复制到另一个目录 - Copy a file from one directory to another in C++ 仅将所需的数据从一个大文件复制到另一个 - Copy only data needed from one big file to another 将一行文本从文件复制到C ++中的字符串 - Copy one line of text from a file to a string in c++ QFile :: copy create会创建文件的副本还是将内容从一个文件移动到另一个文件? - Will QFile::copy create create a copy of the file or move the contents from one file to another? 将文本从一个文件复制到另一c ++ fstream时出错 - Error copying text from one file to another c++ fstream
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM