liv1n9's blog

By liv1n9, history, 6 months ago, In English,

Hi!

I'm writing a C++ program to generate testcases. It has a class Task which contains:

fstream in, out;

and contructor

Task(string fileIn, string fileOut) {
    this->fileIn = fileIn;
    this->fileOut = fileOut;
    in.open(fileIn.c_str(), ios::in);
    out.open(fileOut.c_str(), ios::out);
}

This class will read input from file fileIn (which was generated), solve problem, then write output to file fileOut. No error so far, but the problem is that the program can just read and write with file, while i'm also wanting to do it with standard input/output stream, so that i can also use the program to submit. All i can do now is using a variable bool isFile and if/else statements, which mean i would have to write the input/output parts twice. I'm thinking about a way which i can change from file stream to standard stream directly but i don't know if it were possible.

If you have an efficient way, please help me :( I appreciate all your help!

 
 
 
 
  • Vote: I like it  
  • +5
  • Vote: I do not like it  

»
6 months ago, # |
  Vote: I like it +8 Vote: I do not like it

replace fstream with istream/ostream and do in.rdbuf(cin.rdbuf());in will be reading from standart input.

Read http://en.cppreference.com or http://www.cplusplus.com/ if you want to do something with STL.

  • »
    »
    6 months ago, # ^ |
      Vote: I like it +5 Vote: I do not like it

    Thank you! That's what i'm needing