-3

This is a home work question, so if you are not a fan of those I understand. Here is my code:

#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;
}

Problem is, though the outputFile is always empty. It never copies any lines from cmdfile1 into the output file. Does anyone know what I am missing here?

Sammy
  • 3
  • 1
  • 2
    Who taught you this: `if (myfile2, line == "add");`? - its actually a valid code, but it seems you don't know what its doing – WhiZTiM Jul 17 '16 at 15:29
  • Honestly, I am trying from research and examples I have found online. I guess I dont know what it was doing... I thought it would analyze the file for the word "add". – Sammy Jul 17 '16 at 15:32
  • You need to stop dreaming. This code doesn't make sense. Preferably consult cppreference and get a [book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – LogicStuff Jul 17 '16 at 15:42
  • hahaha I am using Data Structures and Algorithms Analysis in C++ as my course book. Totally not user friendly. I have gotten more helpful information from youtube, this site, and others than from this book alone. – Sammy Jul 17 '16 at 15:52
  • I think you need to go back to basics. Forget C++ for now. Start by writing a set of instructions that you are confident if carried out will accomplish what you want. This could be in the form of a flow diagram or pseudo code. When you have done that, consider how to implement the process in C++. – Ian Jul 17 '16 at 15:58
  • Well thank you guys so much, I appreciate you guys looking at it. I will start from the basics and go from there. I must have really jacked it up lol. – Sammy Jul 17 '16 at 16:06
  • You're missing some basic stuff - essentially, you need to use a loop, preferably in combination with [std::getline](http://en.cppreference.com/w/cpp/string/basic_string/getline). – pzelasko Jul 17 '16 at 16:10

1 Answers1

0

Try something more like this instead:

#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;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770