# How to read a binary file and write a text file with that data?

Hi,
I have a binary file with uint32 values. i need to read those,convert them to scaled floating point (3 places) and write to a text file. For example:

00 01 23 4F 00 01 24 4D...

is 2 values: 001234F (74575 int) and 0012244D (74829 int), each of 4 bytes and the whole file contains such values. I need to scale these in such a way:

(74575 - 65000)/22500

which is: 0.423

and write them to a file as csv values.Can someone tell me how to do that

Thanks!

###### Who is Participating?

Commented:
Something like this

unsigned int val = 0;
float result = 0.0;

FILE *input = fopen("input.dat", "rb"); // b is for binary
FILE *output = fopen("output.dat", "w");

for (int i = 0; i < 2; ++i) {

result = (float)(val - 65000)/22500;

fprintf(output, "%f\n", result);
}

fclose(input);
fclose(output);
0

Commented:
1. Open the input file in binary mode.
3. convert them to float.
4. Open another file in text mode.
5. write the value.

That's it
0

Commented:
Another thing that I want to tell you is that
binary data 00 01 23 4F will not be 74575 in decimal if you are doing the thing in little-endian machine.

It will be
4F 23 01 00
0

Commented:
two things:

Use "%.3f" format string, or rather

for (int i = 0; i < 2; ++i) {
[...]

fprintf(output, "%.3f\n", result);
}

You also may want to use doubles instead of floats. When you use floats, you save nothing, as when the printf() family is called with a float parameter, it's automatically converted to double.
0

Graphics ExpertCommented:
Hi,

Seems numbers are actually 0001234F (74575 int) and 0001244D (74829 int).

main()
{
unsigned char a,b,c,d;
unsigned long n1, n2;
float f1,f2;
FILE *F;

a=fgetc(F); b=fgetc(F); c=fgetc(F); d=fgetc(F);  // get bytes 1st number
n1=a;                                                             // compute integer value
n1=(n1<<8)+b;                                               // shift left 8 times
n1=(n1<<8)+c;
n1=(n1<<8)+d;
f1 = ((float)n1-(float)65000)/(float)22500;         // apply the formula
printf("\n%6.3f",f1);                                         // just to check
a=fgetc(F); b=fgetc(F); c=fgetc(F); d=fgetc(F); // get bytes 2nd gyte
n2=a;
n2=(n2<<8)+b;
n2=(n2<<8)+c;
n2=(n2<<8)+d;
f2 = ((float)n2-(float)65000)/(float)22500;
printf("\n%6.3f",f2);
fclose(F);

F=fopen("MyNumbers.csv","wt");
fprintf(F,"%8.3f,%8.3f",f1,f2);                          // write csv
fclose(F);
}

Hope it helps.

Jose
0

Commented:
Hi there,

You need to consider the following.
The binary representation (in memory) or as in your case in the file depends of the implementation of the program generating theses numbers.
Knowing that it is 4 bytes per value helps but is not enough.

There are several integer types within a single implementation already, let alone across different programming environments
The only way to find out what integer type suits your binary data is to try and find it empirically.
(your compiler's documentation will help you find the 4-byte integer types, but you may as well try them all)

So try the following and compare the resulting numbers to your expecations (either well known file or plausibility)
(I have copyied rajeev_devin's code - Thanks)

//** try any of these alternative integer types
// typedef int t_MyInt;
// typedef unsigned int t_MyInt;
// typedef long  t_MyInt;
// typedef _int32 t_MyInt;

t_MyInt val = 0;
float result = 0.0;

FILE *input = fopen("input.dat", "rb"); // b is for binary
FILE *output = fopen("output.dat", "w");

for (int i = 0; i < 2; ++i) {

result = (float)(val - 65000)/22500;

fprintf(output, "%f\n", result);
}

fclose(input);
fclose(output);

Cheers Sebastian
0

Graphics ExpertCommented:
Hi,
Clearly the input file is binary big endian and output file is ASCII csv.

So, the data cannot be read with any C or C++ reading command as fread, as Windows expect it to be little-endian.
Despite the interesting strategy of bastibartel, that code doesn't work properly in a little-endian machine like the ones with Pentium CPU.

That's why we should read the file byte by byte ...
a=fgetc(F); b=fgetc(F); c=fgetc(F); d=fgetc(F);

... and "construct" the number ...
n1=a;                        // compute integer value
n1=(n1<<8)+b;          // shift left 8 times is the same of multiply by 256
n1=(n1<<8)+c;
n1=(n1<<8)+d;          // at this point, n1 is the number

... for the math operation ...
f1 = ((float)n1-(float)65000)/(float)22500;   // f2 calculated same way

... and save the numbers with 3 decimal places, coma separated:
fprintf(F,"%8.3f,%8.3f",f1,f2);

Jose
0

Commented:
... And how is that so 'clear' I wonder curiously ?

Cheers,
Sebastian
0

Graphics ExpertCommented:
Hi, Sebastian,
For me it's clear by this simple reason:

The author describes the input data as  "00 01 23 4F 00 01 24 4D...", "and the whole file contains such values".
If the bytes are in such sequence, then this is big-endian (BE for short).

Also, the code provided by rajeev_devin with the corrections by stephan73 is good enough and the question would PAQ, if the numbers would little-endian (LE for short).

We can, in this point, be confuse... actually the CPU (in case of Windows + Pentium) is LE. The file (as decribed by the author) is BE. So, I suggest to read the BE data, turn it LE, then call the C math.

Please note I didn't affirm that your code is bad and will not run in a LE CPU. My point is that fread waits for a LE number and what the file has is BE number.

About your code, a minor adjust I could suggest is that only "typedef unsigned int t_MyInt;"  complys to the author's initial declaration "I have a binary file with uint32 values". But, despite that, I like that strategy and find it pretty creative.

Jose

0

Author Commented:
Hi,
Many thanks for all your answers and the differerent prespectives. I found them all immensely helpful. The endianness was an issue and I solved by using a combination of Jose's and Rajeev's answers. I tried to use all the suggestions to write this code of mine.It works well except that float values that are negative are not showing up properly. How does float take care of negative values? any other suggestions are appreciated.Thanks.
-Tanuj.

void main()
{
typedef unsigned int t_uint32;
typedef unsigned char t_uint8;

t_uint32 val = 0;
t_uint8 byte=0;
float result = 0.0;

FILE *file1_in = fopen("file1_in.bin", "rb"); // b is for binary
FILE *file1_out = fopen("file1_out.csv", "wt");

while(!feof(file1_in)){
for (int i = 0; i < 4; ++i) {
val=(val<<8) + byte;
}
result = (float)(val - 75000)/22500;
fprintf(file1_out, "%f\n", result);
val=0;
}
fclose(file1_in);
fclose(file1_out);
}
0

Author Commented:
Ok,silly question, just casting val to float does it. Anyways ,thanks guys!

Rgds,
Tanuj.
0

Commented:
Hello Jose,

I really did not know how to tell LE from BE - so thanks for the clarification. And I never took your comment as criticism :-)
It is always interesting to see though how various answers to the same problem co-exist on totally different abstraction levels.

You are of course right about the Uint issue - that had escaped my attention. (plus, I wanted to give a slightly bigger picture ;-))

Cheers Sebastian
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.