Computing.Net > Forums > Programming > Working With Files in Binary in C++

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

Working With Files in Binary in C++

Reply to Message Icon

Name: Mark Lessel
Date: November 20, 2002 at 17:10:03 Pacific
OS: Windows XP Pro
CPU/Ram: 1.8 GHz P4/512MB SDRAM
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: JDNEal
Date: November 22, 2002 at 10:50:13 Pacific
Reply:

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.


0
Reply to Message Icon

Related Posts

See More


Learning FTP in Visual Ba... A Job Question [shouldn't...



Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Working With Files in Binary in C++

File Descriptors in C?? www.computing.net/answers/programming/file-descriptors-in-c/10512.html

C++ array (works with average) Need www.computing.net/answers/programming/c-array-works-with-average-need/12656.html

Find oldest file in folder www.computing.net/answers/programming/find-oldest-file-in-folder/14755.html