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?
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?
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
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.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?
Jim
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
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
Yes1.I wrote the structure using fwrite. So you mean if I write the structure using fwrite I can only read it using fread??
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 so how can I read single strings/lines/words??
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
The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss
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.
If you know exactly where B is located within the file, yes you can seek to that location and read that information.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]?
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
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.
Hmm, thanks for the information. But am having trouble, so am trying to switch to fprintf and fgetssmokeyangel