Thread: reading from binary files

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    82

    reading from binary files

    Hey guys.

    I have a structure stored in a binary file, now I want to read each string from it (or line), can I do that using fgets?

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Yes. Do not forget to select the appropriate mode in fopen.
    "In order to open a file as a binary file, a "b" character has to be included in the mode string. This additional "b" character can either be appended at the end of the string (thus making the following compound modes: "rb", "wb", "ab", "r+b", "w+b", "a+b") or be inserted between the letter and the "+" sign for the mixed modes ("rb+", "wb+", "ab+")."
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    I have a structure stored in a binary file, now I want to read each string from it (or line), can I do that using fgets?
    It might be possible, it really depends on how you wrote the information to the file. Normally you don't use formatted IO (fgets, fprintf) when using binary files, you normally use fwrite/fread.

    Jim

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I strongly agree with jim!
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    Registered User
    Join Date
    Sep 2013
    Posts
    82
    Quote Originally Posted by jimblumberg View Post
    It might be possible, it really depends on how you wrote the information to the file. Normally you don't use formatted IO (fgets, fprintf) when using binary files, you normally use fwrite/fread.

    Jim
    I wrote the structure using fwrite. So you mean if I write the structure using fwrite I can only read it using fread?? If so how can I read single strings/lines/words??

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    I wrote the structure using fwrite. So you mean if I write the structure using fwrite I can only read it using fread??
    Yes1.

    If so how can I read single strings/lines/words??
    You read() exactly what you write(). So if you write() a structure, you read the structure then use the values that are contained within that structure.

    If you want to read single strings then write the single strings.

    1. While it is possible to write a structure and then read a single character or a sequence of characters from the file using the read() function you will probably run into problems because of structure padding and other issues. It is much easier to read() exactly what you write().

    Jim

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Harith View Post
    I wrote the structure using fwrite. So you mean if I write the structure using fwrite I can only read it using fread?? If so how can I read single strings/lines/words??
    If you're so keen to read it as strings/lines/words then why did you write it as a binary file to begin with? It seems you have a design error. If you explain exactly what you're trying to do a better solution may be offered.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  9. #9
    Registered User
    Join Date
    Sep 2013
    Posts
    82
    Quote Originally Posted by oogabooga View Post
    If you're so keen to read it as strings/lines/words then why did you write it as a binary file to begin with? It seems you have a design error. If you explain exactly what you're trying to do a better solution may be offered.
    I am storing a file name and after that file name I am storing user's name and password, now I want to see if a specific file name exists in that file, and if it does then the program prompts the user for name and password, before user can open that file and read from it.

    One more question: If I store two structures in one file. One structure is A and another is B. The file is mostly made up of A, from start till end, except that B is somewhere in that file. Now if I want to see B, can I just use;
    [CODE]fread(&structure, sizeof(structure), 1, FILE *somefile);
    output B;[\CODE]?

    And thanks guys for barring with me .
    Last edited by Harith; 09-07-2013 at 03:36 AM.

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    If I store two structures in one file. One structure is A and another is B. The file is mostly made up of A, from start till end, except that B is somewhere in that file. Now if I want to see B, can I just use;
    [CODE]fread(&structure, sizeof(structure), 1, FILE *somefile);
    output B;[\CODE]?
    If you know exactly where B is located within the file, yes you can seek to that location and read that information.

    I really suggest you rethink your reasoning behind using the binary file. Why did you decide on the binary file? From your description it doesn't seem like you need random access, you're going to be searching thru the file one record at time for the proper user. And just using a binary file doesn't prevent others from reading your password information, you still will need to encrypt this data.


    Jim

  11. #11
    Registered User
    Join Date
    Sep 2013
    Posts
    82
    Quote Originally Posted by jimblumberg View Post
    If you know exactly where B is located within the file, yes you can seek to that location and read that information.

    I really suggest you rethink your reasoning behind using the binary file. Why did you decide on the binary file? From your description it doesn't seem like you need random access, you're going to be searching thru the file one record at time for the proper user. And just using a binary file doesn't prevent others from reading your password information, you still will need to encrypt this data.


    Jim
    I am just practicing and learning on file I/O, and am using structures so I thought it might be best using fread() and fwrite().

  12. #12
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Quote Originally Posted by Harith View Post
    I am just practicing and learning on file I/O, and am using structures so I thought it might be best using fread() and fwrite().
    Careful. fread() and fwrite() do come with some pitfalls. Only use binary files if you don't care about portability. For example, if you compiled your program on Linux and Windows, they wouldn't be able to correctly read each others binary files.
    If you make any changes to your structures in code, you can't read the binary file anymore. You'd have to keep the old structure, read the data into that, convert it to the new structure, and write it back out,
    For strings, if you have char arrays in your struct then it will just work. However if you have char pointers, which at some point down the line you almost certainly will, it won't work (the strings won't be written out, as fwrite will just write the pointer value).

    I'd use a text file for a problem like this rather than a binary file, just because you're dealing with strings, so it fits. Nothing wrong with using binary files here to learn about fread() and fwrite() though, so long as you can live with the caveats.

  13. #13
    Registered User
    Join Date
    Sep 2013
    Posts
    82
    smokeyangel
    Hmm, thanks for the information. But am having trouble, so am trying to switch to fprintf and fgets

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading binary files
    By rlesko in forum C Programming
    Replies: 13
    Last Post: 11-24-2010, 12:06 AM
  2. Reading binary files
    By stevesmithx in forum C Programming
    Replies: 20
    Last Post: 03-07-2008, 12:48 PM
  3. Reading Binary files
    By earth_angel in forum C++ Programming
    Replies: 10
    Last Post: 07-12-2005, 06:48 AM
  4. reading binary files
    By AmazingRando in forum C Programming
    Replies: 3
    Last Post: 10-09-2003, 12:06 PM
  5. reading old dos binary files
    By ronin in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 11-10-2001, 12:43 PM

Tags for this Thread