Thread: Micosoft Visual C++ 2010 Express writing to a file?

  1. #1
    Registered User
    Join Date
    Sep 2010
    Location
    London
    Posts
    41

    Micosoft Visual C++ 2010 Express writing to a file?

    Hi guys,

    Does anybody know how to write all output to a file please? But trick is I am using Micosoft Visual C++ 2010 Express. There must be an options somewhere? I've not used it very long and don't know how to do this.

    Thank you

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What do you mean - write output to a file?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Location
    London
    Posts
    41
    I meant instead of all cout going to a console for them to go to a file, so that I can keep it and read again if necessary.

    Thank you,

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    cout is made to, by default, output to the console. You shouldn't change that.
    You should instead learn to use iostreams, ofstream to write to a file.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Sep 2010
    Location
    London
    Posts
    41
    OK, I didn't know it was bad practice to do that, will keep it in mind.

    Currently I am using an ofstream for 1 of the outputs:

    Code:
    ofstream outputFile;
    outputFile.open ("MSE.xls");
    // code
    outputFile << MSE << endl;
    outputFile.close();
    But I also need to output a few other things to a file.
    So can I output several things to different files like this:

    Code:
    ofstream outputFile;
    ofstream fileTwo;
    outputFile.open ("MSE.xls");
    fileTwo.open("weights.xls");
    // code
    outputFile << MSE << endl;
    fileTwo << weight[i] << endl;
    outputFile.close();
    fileTwo.close();
    // etc like this for several files to which I'd like to write
    Is that correct?

    Thank you Elysia,

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, but you should also be able to shorten the code into this:
    Code:
    ofstream outputFile("MSE.xls");
    ofstream fileTwo("weights.xls");
    // code
    outputFile << MSE << endl;
    fileTwo << weight[i] << endl;
    // etc like this for several files to which I'd like to write
    Note that there is no need to close. The files will automatically close once they go out of scope. This is an important technique called RAII.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Sep 2010
    Location
    London
    Posts
    41
    OK thank you! Will greatly help.
    Kind Regards,

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Using 'if' with char arrays or string objects
    By c++_n00b in forum C++ Programming
    Replies: 36
    Last Post: 06-06-2002, 09:04 PM