OK, so I know that I can search binary records in C, by entering the number and returning the corresponding data, however I need to search a binary file using text. In my opinion I would need to load this file into a buffer and search the buffer for the required output. My problem is, if that idea is correct, I have no idea what I am doing. Can someone help please?
MichaelSammels 0 Light Poster
Recommended Answers
Jump to Post>>And it outputs junk (mainly numbers like -147, 70, 8, 10), etc.
Exactly -- binary files can not always be read, which is why they are called binary files. You have to know how the binary file is formatted before you can make any sense out of them. For …
Jump to PostAll you have to do is replace fwrite() with fread(). I assume data is a struure with fixed-length character arrays instead of pointers.
Jump to PostSince you are reading the file you need to open it with "rb" instead of "ab"
Also check the return value of fread() to see how many bytes were actually read.
Jump to PostThe file probably contains more than one record, so you have to use a loop to read each of the records until end-of-file is found.
while( fread(&data, sizeof data, 1, file) == sizeof(data) ) printf("%s\n", data.title);
Jump to PostOops! should have been
while(fread(&data, sizeof data, 1, file) > 0)
All 30 Replies
chrjs 42 Junior Poster in Training
MichaelSammels 0 Light Poster
Adak 419 Nearly a Posting Virtuoso
MichaelSammels 0 Light Poster
chrjs 42 Junior Poster in Training
Adak 419 Nearly a Posting Virtuoso
MichaelSammels 0 Light Poster
Adak 419 Nearly a Posting Virtuoso
MichaelSammels 0 Light Poster
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
MichaelSammels 0 Light Poster
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
MichaelSammels 0 Light Poster
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
MichaelSammels 0 Light Poster
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
MichaelSammels 0 Light Poster
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
MichaelSammels 0 Light Poster
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
MichaelSammels 0 Light Poster
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
MichaelSammels 0 Light Poster
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
MichaelSammels 0 Light Poster
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
MichaelSammels 0 Light Poster
MichaelSammels 0 Light Poster
MichaelSammels 0 Light Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.