Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.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++ - Fatal编程技术网

C++:无法将行从输入文件复制到输出文件

C++:无法将行从输入文件复制到输出文件,c++,C++,这是一个家庭作业的问题,所以如果你不是这些我理解的球迷。这是我的密码: #include <fstream> #include <iostream> #include <string> using namespace std; int main() { fstream myfile1("datafile1.txt"); //this just has a bunch of names in it fstream myfile2("cmdfil

这是一个家庭作业的问题,所以如果你不是这些我理解的球迷。这是我的密码:

#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int main()
{
    fstream myfile1("datafile1.txt"); //this just has a bunch of names in it
    fstream myfile2("cmdfile1.txt");  //has commands like "add bobby bilbums"
    ofstream outputFile("outfile1.txt"); //I want to take the "add bobby" command and copy the name into this new file.
    string line;
    if (myfile1.is_open() && myfile2.is_open()) //so I open both files
    {
        if (myfile2, line == "add"); //If myfile2 has an "add" in it
        {
            outputFile.is_open(); //open outputfile
            outputFile << line << endl; //input the line with add in it till the end of that line.
        }
    }
    cout << "\nPress Enter..."; // press enter and then everything closes out.
    cin.ignore();
    outputFile.close();
    myfile2.close();
myfile1.close();
return 0;
}

问题是,尽管outputFile总是空的。它从不将cmdfile1中的任何行复制到输出文件中。有人知道我在这里遗漏了什么吗

试试类似的方法:

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

using namespace std;

int main()
{
    ifstream myfile1("datafile1.txt");
    ifstream myfile2("cmdfile1.txt");
    ofstream outputFile("outfile1.txt");
    string line;

    if (/*myfile1.is_open() &&*/ myfile2.is_open() && outputFile.is_open())
    {
        while (getline(myfile2, line))
        {
            if (line.compare(0, 4, "add ") == 0)
            {
                outputFile << line.substr(4) << endl;
            }
        }
    }

    myfile1.close();
    myfile2.close();
    outputFile.close();

    cout << "\nPress Enter...";
    cin.ignore();

    return 0;
}

谁教你的:如果myfile2,line==add;?-它实际上是一个有效的代码,但你似乎不知道它在做什么。老实说,我正在通过我在网上找到的研究和示例进行尝试。我想我不知道它在做什么。。。我想它会分析文件中的add这个词。你需要停止做梦。这个代码没有意义。我最好使用C++的数据结构和算法分析作为我的课程手册。完全不是用户友好的。我从youtube、本网站和其他网站上获得了比这本书更有用的信息。我认为你需要回到基础上来。现在忘记C++。首先,写一组说明,你确信如果执行了这些说明,就会完成你想要的。这可以是流程图或伪代码的形式。当你这样做时,考虑如何在C++中实现这个过程。谢谢帮助!你把我推向了一个更好的方向!