Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I am trying to write a C++ program and I need to extract data from a file in binary and have sections of it, about 9 8 bit sections (or 9 digits of binary #s), converted into an unsigned long #. I am not sure how to do read/write operations in binary or how to get data from those files. Curently I am trying the get() function. A simple way to explain what I need is... I need to get data from a file in binary, change it when it is in decimal form, then write it to a new file in binary.

Binary reading is done through "fread();".
Binary writing through "fwrite();"(Well, that's what I generally use... There are dozens of ways if you look around...)
What you want is kind of vague, but assuming your data looks like this:
394857098
And you want it to be translated into a number like this:
394,857,098
One way to do it would be to define a nine-digit array, read it and translate it like this:
unsigned char MyNineDigits[9];
long myresult;fread ( MyNineDigits, sizeof(MyNineDigits), 1, filehandle);
myresult = (long) MyNineDigits[0] * 1000000000 +
(long) MyNineDigits[1] * 100000000 +
(long) MyNineDigits[2] * 10000000 +
(long) MyNineDigits[3] * 1000000 +
....etc with the other digits
;Don't use this as gospel - it's the general idea of one way to do it. If you are interpreting the data in a different way, you'd have to change the conversion formula to do it. You may see some typo's in the code example, too, don't stumble on what is exact, but rather use the idea.
Look up "fread();" for more. Setting up your filehandle and such is of course left to you to figure out and debug.

![]() |
Learning FTP in Visual Ba...
|
A Job Question [shouldn't...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |