Thread: Saving numbers to files

  1. #1
    Registered User
    Join Date
    Dec 2007
    Location
    Rochester
    Posts
    40

    Saving numbers to files

    I was wondering if you could save and recover numbers from files. The tutorial didn't really say anything about it. Although it does talk about ASCII.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    you can always use sscanf and sprintf to convert any data to string and when write/read strings using
    fgets/fputs (one way)

    you can use fprintf/scanf pair directly

    or you can use fread/fwrite pair for binary in/out operations if the reading and writing is always performed by the same application on the same platform
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  3. #3
    Registered User
    Join Date
    Dec 2007
    Location
    Rochester
    Posts
    40
    Isn't that C style coding.
    Also, either I don't understand you fully, or I didn't explain my question well enough. I have several numbers that I would like to store to a file so that a user can save their game if they don't complete it.
    Just one more question, is there a way to create a file if it doesn't already exist?

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    70
    To start off this article may be of interest to you.

    http://www.cplusplus.com/doc/tutorial/files.html

    it's pretty basic, and doesn't give examples of writing/reading numbers to/from files, but the functions vart provided should do that. Overall I think it's a pretty good tutorial, it's how I got started with file I/O.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    Quote Originally Posted by cpudaman View Post
    Isn't that C style coding.
    Also, either I don't understand you fully, or I didn't explain my question well enough. I have several numbers that I would like to store to a file so that a user can save their game if they don't complete it.
    Just one more question, is there a way to create a file if it doesn't already exist?
    Ahh, sorry, in this case - just open file as a fstream, and then use it as it is any other istream or ostream you use... (You do know how to use cout/cin, do you?)

    for converting string to int and vise-versa - you can use stringstream

    and of course, if you open file for writing, that does not exists - it is created
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  6. #6
    Registered User
    Join Date
    Dec 2007
    Location
    Rochester
    Posts
    40
    Yes, I do know how to use cin/cout. I will look into that tutorial. Thanks for all the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. pulling numbers from files....
    By Confuzz in forum C++ Programming
    Replies: 3
    Last Post: 09-07-2004, 07:49 PM
  4. reinserting htm files into chm help files
    By verb in forum Windows Programming
    Replies: 0
    Last Post: 02-15-2002, 09:35 AM
  5. saving files/opening files
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-29-2001, 10:16 PM