-1

So my problem is: I need to get some primitive values(floats) from a file I used and parsed in Java to my C code(testing between Java and C implementation). So in Java I used ByteBuffer.allocate() to get bytes and Files.write() to write them in the file. File looks OK, I checked him with Hex Editor(real values, not just zero's). Now in C, for reading the data I used:

float * floatArray = malloc(numData * sizeof(float)); //numData is int, 1000 for example
FILE * stream = fopen(input, "rb"); //input is the name of my binary file
rewind(stream);
fread(floatArray, numData, sizeof(float), stream);
fclose(stream);

and for test:

for(i = 0; i < 128; i++) {
    printf("%f, ", floatArray[i]);
}

but only thing I see is

0.00000000, 0.00000000, 0.00000000, 0.00000000, 0.00000000, 0.00000000, 0.00000000, 0.00000000, 0.00000000, 0.00000000, 0.00000000, 0.00000000, 0.00000000, 0.00000000, + next 114 zeros

For more details:

Input file has like 158 MB. It actually has lot of zeros in it, but for example, first 3 numbers should be 9, 52 and 29. Another thing is that I already checked the return value of fread and it returns the right value (numData). I checked other questions on the site but I didn't really found my problem. Other ones had problems like they set file indicator with fseek at the end of the file when they got all the zero's. I guess it can't be endianess problem too because I think I would get bad values and not zero's only. I use Visual C++ 2008 Express edition on Windows 7.

Sorry for my formatting and english, it's my first question. I'll be thankfull for any kind of help.

ZidaneT
  • 3
  • 2
  • Is `floatArray` the same thing in the for-loop? – BLUEPIXY Nov 17 '14 at 15:53
  • `input is the name of my binary file`. Are you sure you were able to open it? All zeroes *may* indicate the file never got read. Check the return values on `fopen`, remove that `rewind` (you don't need it), and check the return value of `fread`. – Jongware Nov 17 '14 at 16:04
  • I just tried `if(stream == NULL) printf("Error");` and no error printed – ZidaneT Nov 17 '14 at 16:31

2 Answers2

0

Looks like you put size and count in the opposite order (http://en.cppreference.com/w/c/io/fread)

size_t fread(void *ptr, size_t size, size_t count, FILE *stream)

If a partial element is read, its value is indeterminate

Kostya
  • 1,552
  • 1
  • 10
  • 16
  • You are right. But it is not the reason that data becomes 0. – BLUEPIXY Nov 17 '14 at 16:07
  • It is indeterminate since it reads only part of it. Just updated my answer with a more proper c reference – Kostya Nov 17 '14 at 16:14
  • Size to be read as a whole is the same. Should not be "only part of it". also Not related to the sequence of arguments that fail to read. – BLUEPIXY Nov 17 '14 at 16:18
  • just tried fread(floatArray, sizeof(float), numData, stream); still same zero's – ZidaneT Nov 17 '14 at 16:34
  • Could you create a small test data file in Java and post it somewhere? – Kostya Nov 17 '14 at 17:15
  • hey, i just came about this: 5.831e-042#DEN float (found this value in debugger when loading only first float), i searched for #den, means denormalized value (?), and i read from http://en.wikipedia.org/wiki/Denormal_number that there can be flags as denormals-are-zero (DAZ) and flush-to-zero (FTZ). I don't really have good understanding about this, could it be it? And how can I remove these flags? EDIT: so lets say it could be problem of endianess after all what could create these denormal numbers? – ZidaneT Nov 17 '14 at 21:39
  • Maybe, the easiest is to try different endianess in Java, e.g. http://stackoverflow.com/questions/13553349/bytebuffer-little-endian-insert-not-working – Kostya Nov 17 '14 at 23:27
0

So I finally solved it. The problem was in endianess all along. I couldn't see it because the values I was reading were Denormal numbers and my compiler treated them as zero's by default. So I switched byte orders back in Java to generate new binary file to use in C and it works fine.

ZidaneT
  • 3
  • 2