Thread: Saving int to file as text?

  1. #1
    napKINfolk.com napkin111's Avatar
    Join Date
    Apr 2002
    Posts
    310

    Question Saving int to file as text?

    I'm trying to save an int to a file so that it can be read in notepad. Currently it is going to save the score to the file, and I'm probably going to have it so it saves a high score list or something later.
    The problem is that I think it is saving in binary, because when I open it in notepad all I get is the "unsupported character symbol" (looks like a rectangle). Here's what I've got:
    Code:
    int score=0;//this is added up through the game
    DWORD dw=0;
    HANDLE hFile=0;
    hFile=CreateFile("score.txt" ,GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
    WriteFile(hFile,(void *)&score,sizeof(int),&dw,0);
    CloseHandle(hFile);
    Thanks!

    napKIN
    "The best way to get answers is to just keep working the problem, recognizing when you are stalled, and directing the search pattern.....Don’t just wait for The Right Thing to strike you – try everything you think might even be in the right direction, so you can collect clues about the nature of the problem."
    -John Carmack

  2. #2
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Lightbulb Try this...

    First, be sure to include stdio.h:

    FILE *file = fopen("myfile.txt", "wb"); // Open file
    int integer = 100; // Sample integer
    fprintf("%d", integer); // Write to file
    fclose(file);

    That should work =).
    Website(s): http://www16.brinkster.com/trifaze/

    E-mail: [email protected]

    ---------------------------------
    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I've never used CreateFile/WriteFile myself, but according to the helpfiles they can only write binary data, meaning you have to convert your integer value to a string then write each letter in that.

    Or, simply use a C++ filestream and your problems are solved automatically:
    Code:
    int HighScore;
    ofstream WriteFile;
    
    WriteFile.open("MyFile.txt", ios::out | ios::trunc);
    if(!WriteFile.fail())
    {
       WriteFile << "The highscore is: " << HighScore << endl;
       WriteFile.close();
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Re: Try this...

    Originally posted by SyntaxBubble
    First, be sure to include stdio.h:

    FILE *file = fopen("myfile.txt", "wb"); // Open file
    int integer = 100; // Sample integer
    fprintf("%d", integer); // Write to file
    fclose(file);

    That should work =).
    This is a C++ forum, not C
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    Or if you want, you can use a string stream:

    Code:
    #include <strstream>
    
    //Later in code...
    ostringstream oss;  //creates a string stream for output
    
    oss <<number;  //outputs int to the string stream
    oss.str()   //returns the char* representation of the string stream
                    //which you can use to output to file
    But simply using the << operator of an ofstream object would probably be the easiest way

  6. #6
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    This is a C++ forum, not C
    Sure, there're better ways, but it's still C++.
    p.s. What the alphabet would look like without q and r.

  7. #7
    napKINfolk.com napkin111's Avatar
    Join Date
    Apr 2002
    Posts
    310
    Thank you for the suggestions. I think I'm going to try Magos's technique first.

    Magos, have you started on OpenGL? (I know you were doing DX stuff) Just curious.

    //napKIN
    "The best way to get answers is to just keep working the problem, recognizing when you are stalled, and directing the search pattern.....Don’t just wait for The Right Thing to strike you – try everything you think might even be in the right direction, so you can collect clues about the nature of the problem."
    -John Carmack

  8. #8
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by napkin111
    Magos, have you started on OpenGL? (I know you were doing DX stuff) Just curious.
    Nope, no OpenGL. I'm working on a game in DX. Progress is slow though...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Just curious, how did you decide to use directX versus openGL?

  10. #10
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by 7stud
    Just curious, how did you decide to use directX versus openGL?
    I was in my early gamecreator wannabe stages (like most programmers ) and simply decided to use DX. Didn't know much about them, so i guess it was just random.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 07:34 AM