Thread: Using data from binary files?

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    17

    Using data from binary files?

    I'm absolutely sure this would be possible, but how would one (if one could) go about putting variable data from a binary file (output via stdio.h functions like 'fopen' and 'fprintf' with the 'wb' mode) into a variable within a program? I'm basically trying to use this file as a 'config' file for a pc game I'm working on (this config file would be important for storing universal/multi-user settings, like the game's resolution and keyboard, etc), and this information would be additionally-useful in setting up my save system. I've been looking around, but I can't seem to find how to load such data into a variable within the program (either that, or I'm an oblivious moron who has repeatedly overlooked it). If anyone could point me in the right direction with this, you'll be a life-saver!

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Only if you know what type of variable: int, struct, union, etc. was used to fwrite() data into the file to begin with.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    17
    Well, that verifies that it can be done, but I was asking how it would work. Do I just fread the file and then define the variable I want to fill with the variable read from the file (as if it were declared and defined from within the program, itself)?

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    If there is no reason not to, it's usually best to use a text file, not a binary file, since it's more portable and can be read and edited by a human (good for debugging, for example).
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    If you insist on reading/writing binary files you may want to start by studying this tutorial. C File I/O Tutorial. But as already pointed out a text file may be a better option.

    Jim

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    17
    I guess people on Windows wouldn't be able to edit the contents of their files, but I've been having no trouble at all on Linux Mint. I'll change the output mode, then, thanks! So, back to my actual question, then: Do I treat the variables stored within an opened-for-reading file as though they were native to the program, itself?:
    int A = 1;
    int B = A;
    ...or is there a different procedure for this sort of thing?

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    So you're saying you want the 'config' file to contain C-like statements, specifying variable name and value.
    NO you can not dynamically create variables according to names specified externally. You have to define generic variables or arrays, structs, etc., which will hold name, value. I assume this is some sort of calculator perhaps. Then you have to be able to search for existing names, add new names, etc. So you'll have a list of variable names in an array.

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Abstracticus View Post
    I guess people on Windows wouldn't be able to edit the contents of their files, but I've been having no trouble at all on Linux Mint.
    That makes no sense. Remember that even if you open a file in "binary" mode, if you output with fprintf you aren't really writing binary data! Binary data is written like this:
    Code:
    FILE *fp = fopen("afile", "wb");
    int n = 123456789;
    int m = 987654321;
    fwrite(&n, sizeof n, 1, fp);
    fwrite(&m, sizeof m, 1, fp);
    fclose(fp);
    Try editing that!!!

    It would be read like this:
    Code:
    FILE *fp = fopen("afile", "rb");
    int n, m;
    fread(&n, sizeof n, 1, fp);
    fread(&m, sizeof m, 1, fp);
    fclose(fp);
    printf("n=%d  m=%d\n", n, m);
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  9. #9
    Registered User
    Join Date
    Oct 2010
    Posts
    17

    SOLVED: Using data from binary files?

    Well, it can't be edited in a text editor, but it seems to work beautifully! This config file is created and set up with defaults whenever the game is run without finding a pre-existing config file, and all aspects which are okay for editting (meaning things which aren't system/hardware-reliant and automatically set up by detection) can be edited via the config menu (input, for instance, will need be to set up before all else to ensure the best experience for each keyboard layout). That tutorial has been bookmarked for reference, and I think we can call this inquiry 'resolved' now! ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary data handling
    By maverickbu in forum C Programming
    Replies: 1
    Last Post: 06-26-2007, 01:14 PM
  2. Binary data
    By tehuknownjedi in forum C++ Programming
    Replies: 7
    Last Post: 04-16-2006, 02:51 PM
  3. Binary data -> istream
    By Magos in forum C++ Programming
    Replies: 8
    Last Post: 01-24-2005, 02:57 AM
  4. binary data
    By sycorax in forum C++ Programming
    Replies: 16
    Last Post: 03-19-2004, 02:37 PM
  5. Write and use data stored in data files...
    By alex6852 in forum C++ Programming
    Replies: 2
    Last Post: 11-01-2001, 01:45 PM

Tags for this Thread