Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将文本从一个文件复制到另一个c++;fstream_C++_File - Fatal编程技术网

C++ 将文本从一个文件复制到另一个c++;fstream

C++ 将文本从一个文件复制到另一个c++;fstream,c++,file,C++,File,这是我的代码 #include <iostream> #include <fstream> #include <string> int main() { std::fstream file; file.open("text.txt", std::fstream::in | std::fstream::out | std::fstream::app); if(!file.is_open()) {

这是我的代码

#include <iostream>
#include <fstream>
#include <string>

int main()
{
    std::fstream file;
    file.open("text.txt", std::fstream::in | std::fstream::out | 
              std::fstream::app);
    if(!file.is_open())
    {
        std::cout << "Could not open file(test.txt)" << std::endl;
    } else {
        file << "These are words \nThese words are meant to show up in the new file \n" << 
                "This is a new Line \nWhen the new fstream is created, all of these lines should be read and it should all copy over";

        std::string text;
        file >> text;
        std::cout << text << std::endl;
        file.close();

        std::fstream newFile;
        newFile.open("text2.txt", std::fstream::in | std::fstream::out | 
                     std::fstream::app);

        if(newFile.is_open())
        {
            newFile << text;
        }
    }
}
#包括
#包括
#包括
int main()
{
std::fstream文件;
file.open(“text.txt”,std::fstream::in | std::fstream::out |
std::fstream::app);
如果(!file.is_open())
{

std::cout您正在尝试从文件末尾读取。该位置设置为您最后写入文件的内容的末尾,因此,如果要读取您写入的内容,必须重置它:

file.seekg(0);

这会将输入的位置设置回文件的开头。但是请注意,现在这样读取文件只会得到1个单词(直到第一个空格)。如果您想全部读取,可能应该查看以下内容:。

您正在尝试从文件末尾读取。位置设置为上次写入文件的结尾,因此,如果您想读取写入的内容,必须重置它:

file.seekg(0);

这会将输入的位置设置回文件的开头。但是请注意,现在这样读取文件只会得到1个单词(直到第一个空格)。如果您想全部读取,可能应该查看以下内容:。

当您将字符串附加到
fstream
时,输入/输出位置设置为文件的结尾。这意味着您下次读取文件时,将看到的只是一个空字符串

您可以使用以下方法检查当前输入位置:

file.tellg()
file.seekg(0)
并使用以下方法将输入/输出位置设置为启动:

file.tellg()
file.seekg(0)

std::fstream
的完整引用是。

当您将字符串附加到
fstream
时,输入/输出位置设置为文件的结尾。这意味着您下次读取文件时,将看到的只是一个空字符串

您可以使用以下方法检查当前输入位置:

file.tellg()
file.seekg(0)
并使用以下方法将输入/输出位置设置为启动:

file.tellg()
file.seekg(0)

std::fstream
的完整参考是。

文件>>文本;
即使你倒带,也只能得到第一个单词
文件>>文本;
即使你倒带,也只能得到第一个单词