0

This is what I currently have:

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

using namespace std;

int main(){

fstream bookings("Schedule.txt");
fstream magicians("Magicians.txt");
fstream holidays("Holidays.txt");

//already tested to make sure these are open & they are

string test;
string holiday;

bookings >> test;

if(test.length()==0){
   while(getline(holidays, holiday)){
       bookings << holiday;
       cout << "test";

        }
    }

bookings.close();
magicians.close();
holidays.close();


  return 0;
}

My Holidays.txt contains this:

Veteran's Day
Valentine's Day
Halloween
Christmas
New Years

I've made sure my files are in the right directory and that the files are actually open. I stepped through the program to make sure holiday is getting the line accordingly but bookings << holiday; just doesn't seem to work properly for me?

conejo
  • 31
  • 6
  • Check file permissions? Should you add a << endl; to when you write to bookings? – tzengia Feb 11 '19 at 01:42
  • 1
    It's probably not a good idea to try to read from and write to a file at the same time. Do you just need to check if a file is empty? Try closing the file and reopening it before you write to it. – eesiraed Feb 11 '19 at 01:44
  • @FeiXiang it seems like he's trying to append the holiday file to the bookings file – tzengia Feb 11 '19 at 01:46
  • OP, are you using Windows? Maybe take a look at this: https://stackoverflow.com/questions/17536570/reading-and-writing-to-the-same-file-using-the-same-fstream#17567454 – tzengia Feb 11 '19 at 01:49
  • I'm using xcode. I had `<< endl;` previously too but that didn't work. The end goal of the program is to create a schedule with the holidays and then whatever magician are scheduled that day under them, hence I was importing the holidays first then I would check to see whether they're scheduled a certain day and then input the magician names into the schedule file accordingly. – conejo Feb 11 '19 at 02:16

2 Answers2

0

Hope this helps.

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

using namespace std;

int main(){

    fstream bookings("Schedule.txt", ofstream::app);
    fstream magicians("Magicians.txt");
    fstream holidays("Holidays.txt");

    string holiday;

    while(getline(holidays, holiday)){
       bookings << holiday << "\n";
       cout << holiday << "\n";

    }

    bookings.close();
    magicians.close();
    holidays.close();

  return 0;
}
donpsabance
  • 358
  • 3
  • 9
  • Worked perfectly, thanks! Mind explaining what exactly ofstream::app is doing? – conejo Feb 12 '19 at 04:24
  • Oh sorry about that, basically it will append the text to the file (adding at the end) rather than just rewriting over the previous contents, if that makes sense. If not [here](http://www.cplusplus.com/reference/fstream/ofstream/open/) is more info. – donpsabance Feb 12 '19 at 04:32
0

Have you tried declaring your ifstream and ofstream variables first like you see below?

ifstream inFile; ofstream outFile;

I use the following to open the input file before inputing contents into the stream.

inFile.open("input.txt"); outFile.open("toutput.txt");

I'm new to C++. Hope this helps.

Cornel
  • 180
  • 2
  • 4
  • 13