Outputing Results to a File

This is a discussion on Outputing Results to a File within the C++ Programming forums, part of the General Programming Boards category; Heya folks I need help. This is probably stupidly dumb and simple but i need it non the less. I'm ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    7

    Exclamation Outputing Results to a File

    Heya folks I need help. This is probably stupidly dumb and simple but i need it non the less. I'm currently doing an assignemt for a temperature converter. It goes like so:

    Input Number
    Convert to (lets just say err Fahrenheit)
    Output result Onscreen with cout

    What i'd like to do to expand my program is to also make my program write this result to a .txt file. Kind of like a log file. Can anyone help? This is very urgent to me

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >What i'd like to do to expand my program is to also make my program write this result to a .txt file.
    Code:
    #include <fstream>
    using namespace std;
    .
    .
    char filename[] = "temp_log.txt";
    ofstream out(filename);
    out << Fahrenheit << endl;

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Don't you need something else? Like whatever the command is to make it append the file and not erase it every time and then write it.
    My computer is awesome.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    7
    Quote Originally Posted by swoopy
    >What i'd like to do to expand my program is to also make my program write this result to a .txt file.
    Code:
    #include <fstream>
    using namespace std;
    .
    .
    char filename[] = "temp_log.txt";
    ofstream out(filename);
    out << Fahrenheit << endl;
    Ok the namespace std? Is that what i typicalyl use as main() ?
    and out << Fahrenheit is cout << Fahrenheit ?

    and Thanks for the very quick reply

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    7
    Quote Originally Posted by cerin
    Don't you need something else? Like whatever the command is to make it append the file and not erase it every time and then write it.
    Oh yea that'd be most imprtant. I'd like it to log each result one after another kinda like:

    212 Fahrenheit
    123 Celcius
    342 Absolute


    That's how my results are formatted. I used a string copy, so when the user chooses which they want to convert to the program writes the string as the temperature type it's converting to.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >make it append the file and not erase it every time and then write it.
    Well, to append use:
    Code:
    ofstream out(filename, ios::app);
    >Ok the namespace std? Is that what i typicalyl use as main() ?
    A short program would look like this, unless you want to append std:: to anything from the C++ standary library. Maybe you've got an old compiler?
    Code:
    #include <fstream>
    using namespace std;
    
    int main()
    {
       char filename[] = "temp_log.txt";
       ofstream out(filename, ios::app);
       out << Fahrenheit << endl;
    }
    >out << Fahrenheit is cout << Fahrenheit ?
    cout << Fahrenheit; // Writes to the console (Console Out)
    out << Fahrenheit; // Writes to a file in this example
    Last edited by swoopy; 03-03-2005 at 03:56 PM. Reason: Open file for append.

  7. #7
    Registered User
    Join Date
    Mar 2005
    Posts
    7
    Ahh great Thanks Snoopy & Cerin. I'll go try that right now *Fingers Crossed*

  8. #8
    Registered User
    Join Date
    Mar 2005
    Posts
    7

    Genious :d

    Thanks guys it's worked! Thanks so much It's now appending which is great

    It looks like this so far:

    251.60012FF10 - Fahrenheit

    I only want it to show 251.6. Sounded simple enough to fix so by altering the declared "Result" to "Result[4]" but it gives me apile of errors. X_X Yikes. Anyone have any ideas?
    Last edited by SITHDUKE; 03-03-2005 at 04:06 PM.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >I only want it to show 251.6.
    If your temperature is a double or float, you can do:
    Code:
    out << fixed << setprecision(1) << Fahrenheit << endl;
    Make sure to:
    Code:
    #include <iomanip>
    at the top.

  10. #10
    Registered User
    Join Date
    Mar 2005
    Posts
    7
    out << fixed << setprecision(1) << Result << out << " - " << Temp_Type << endl;
    That's my out. Unfourtunatly it's still giving
    "212.00012FF10 - Fahrenheit"

    Also it's a Float.
    Last edited by SITHDUKE; 03-03-2005 at 04:20 PM.

  11. #11
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Code:
    out << fixed << setprecision(1) << Result << out << " - " << Temp_Type << endl;
    Get rid of that out in red above. That extra out is causing the bad data in your file, it is likely that the value displayed is the address of the out stream object.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  12. #12
    Registered User
    Join Date
    Mar 2005
    Posts
    7
    Ah Thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21