1

I can't understand why this little C code doesn't work

#include <stdio.h> main(int argc,char **argv){ FILE *fp, int i; size_t elem_read; int buffer[100]; fp=fopen(argv[1],"r"); elem_read=fread(buffer,sizeof(int),100,fp); for(i=0;i<elem_read;i++) fprintf(stderr,"%d\t",buffer[i]); fclose(fp); } 

To shorten the code I haven't done any error checking but it should work...I have tried it with both txt and bin file to read numbers and print them out. I think I understand why this doesn't work with txt files, but I dont understand why it doesn't with .bin files? I have a file that contains 4 ints: 10 10 10 10, but when I try to run it with ./a.out file.bin I get some randomg numbers(garbage output), Where's the problem?

4
  • what is the content of the file? Commented Feb 9, 2015 at 18:05
  • Text? 10 10 10 10? Commented Feb 9, 2015 at 18:06
  • @BsD: and what is your program's output? Commented Feb 9, 2015 at 18:06
  • 2
    How did you generate the file? If you stuck a bunch of 10s in there, they aren't integers. You need to know the width of an integer and then put the appropriate binary in the file. more than likely that would be \xa00000 Commented Feb 9, 2015 at 18:07

1 Answer 1

6

The reason it does not work with .bin file is that if you can see 10 10 10 10 in your text editor, you've got a text file with a .bin extension. The values that you read are not "garbage", though: they are bytes that represent the text in the encoding of your file, re-interpreted as integers.

In order to read the numbers back as ints, write a program that writes binary numbers to a file, like this

FILE *fp = fopen("test", "wb"); int[] data = {10, 10, 10, 10}; fwrite(data, sizeof(int), sizeof(data)/sizeof(int), fp); fclose(fp); 

and then use your program to read them.

Sign up to request clarification or add additional context in comments.

5 Comments

Important note: When accessing (reading, writing or both) binary files, add "b" to the mode (that is, use "rb" or "wb"), otherwise your data may get corrupted. That's because just "r" may (for example --- actual depends on OS) convert CRLF (0D,0A in source) to just LF (0A) --- in other words, having you drop bytes. In old Mac OS systems, it would convert CR to LF (0D->0A). When dealing with text files, I always read in binary mode myself, and deal with the possible line endings manually; less to go wrong if users give you files with incorrect (or even mixed) line endings.
@TimČas yes, I know, but since I'm on Linux I dont bother for simple programs like this
@BsD: Of course, but it's good practice to write portable programs; especially when doing so is literally one keystroke away!
@dasblinkenlight What do you exactly mean by seeing 10 10 10 10 in text editor? Do you mean seeing if I open .bin file? I can only those 10's in my text editor just before saving it as .bin file, after I saved I cant see it, is that how it should be?
@BsD No, you should not be able to enter or see numbers in a binary file as numbers. Properly saved binary numbers will look like garbage in a text editor. You should be able to view them in binary (or hex) viewers, which display individual bytes. When you save four tens in a binary file, it should look like 0A 00 00 00 0A 00 00 00 0A 00 00 00 0A 00 00 00 or like 00 00 00 0A 00 00 00 0A 00 00 00 0A 00 00 00 0A, depending on whether your hardware is little endian or the big endian.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.