Thread: Logging Data

  1. #1
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644

    Logging Data

    I've tried a lot of times to create a logging function (using \n to go to the next line), but with no luck. Here's my logging function:
    PHP Code:
    #include <fstream.h>

    charszDataToLog(charData)
    {
        
    ofstream fout;
        
    fout.open("log.txt");
        
    fout << Data << "\n";
        
    fout << flush;
        
    fout.close();
        return 
    Data;

    and here's how I use it:
    PHP Code:
    szDataToLog("data\n"); 
    My problem is that when I do this, when I call it another time, it deletes "data" with the new text.

    Anyone got any ideas on how to fix this?

  2. #2
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    It has something to do with the way your opening the file. Your opening it in write mode, but it should be opened in append mode. I think it should look something like this:

    ofstream fout("log.txt", ios::append);

    But I'm not sure if its ios::append or not, but give that a go. Its something like that anyway.

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    I believe it's ios::app. Also, use code tags, not PHP tags. Unless you're posting PHP code that is.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Try
    Code:
    fout.open("log.txt", ios::app);

  5. #5
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Originally posted by XSquared
    I believe it's ios::app. Also, use code tags, not PHP tags. Unless you're posting PHP code that is.
    That worked, thank you.

    Sorry, I just like the cool coloring [heh].

    [EDIT: Does anyone know why I can't view the class members? I'm using MSVC++ 6.0 Enterprise].

  6. #6
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Ahh there we go, knew it was something like that.

    Why don't you guys like code tags? Personally I think they are brilliant for any sort of code, simply because its coloured and so easier to follow. Don't you guys work with any sort of syntax highlighting? Or is that just for us non-pros out there

  7. #7
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Originally posted by stovellp
    Ahh there we go, knew it was something like that.

    Why don't you guys like code tags? Personally I think they are brilliant for any sort of code, simply because its coloured and so easier to follow. Don't you guys work with any sort of syntax highlighting? Or is that just for us non-pros out there
    Code tags ain't colored, PHP tags are.

    One more thing, is there a way to clear the file of data, and then write the stuff again?
    (EX: Someone runs my program, it saves data. The person runs the program again, it clears the data, and then writes it again)

  8. #8
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    At the start of your program, just do this:
    Code:
    fout.open( "log.txt" );
    fout.close( );
    And it should clear it out.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  9. #9
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    io open_mode

    ios::trunc - Deletes all previous content in the file

  10. #10
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Originally posted by XSquared
    At the start of your program, just do this:
    Code:
    fout.open( "log.txt" );
    fout.close( );
    And it should clear it out.
    Yup, that worked, thank you.

    [EDIT]: ios::trunc deletes everything except the last line (I have about 4-6 lines to write at the moment to the file). Thanks though.
    Last edited by Quantrizi; 08-09-2003 at 10:07 AM.

  11. #11
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Ops, its late, when I said 'code' tags it should have read PHP tags. I'm going to make a thread about this in the general discussions forum tommorow to see what people think.

  12. #12
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    [EDIT]: Does anyone know how to get the last error from GetLastError and turn it into a string? :[/EDIT]

    Does anyone know about the not being able to see class members?

    [When I do
    Code:
    ofstream fout;
    fout.open("...");
    .....
    fout.close();
    VC++ won't display class members of fout, nor any other members.]
    Last edited by Quantrizi; 08-09-2003 at 10:26 AM.

  13. #13
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I looked at the docs on GetLastError(), and at the bottom in the "See also", it listed FormatMessage(). I'm not 100% sure that's what you're looking for, but it's probably worth looking into. Or, of course, you could do a switch

    **EDIT**
    Just so you know, you should be including <fstream> and not <fstream.h>, and then put "using namespace std;" at the top of your code file.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  14. #14
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Originally posted by Hunter2
    I looked at the docs on GetLastError(), and at the bottom in the "See also", it listed FormatMessage(). I'm not 100% sure that's what you're looking for, but it's probably worth looking into. Or, of course, you could do a switch
    Thank you, I'll look into that, and post to tell what happens.

  15. #15
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    #include <windows.h>
    #include <stdio.h>
    
    void DisplayError( LPSTR msg, DWORD err )
    {
    	char msgbuf[4096];
    
    	sprintf(msgbuf,"%s\nWindows Error %d: ",msg, err );
    
    	FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, err,
    		MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT),
    		msgbuf + lstrlen(msgbuf), sizeof( msgbuf ) - lstrlen(msgbuf), NULL );
    
    	MessageBox(NULL,msgbuf,"Error",MB_ICONERROR);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  4. C Programming Question
    By TK in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 07-04-2002, 07:11 PM
  5. File Database & Data Structure :: C++
    By kuphryn in forum C++ Programming
    Replies: 0
    Last Post: 02-24-2002, 11:47 AM