reading from binary files 
Author Message
 reading from binary files

Hi,

I have written some code to read binary data from a file and convert it
into the appropriate data type for use in my program.  I have written
routines that get shorts, longs, floats and doubles.  It seemed silly to
write a routine for getting chars, since "getc" works fine.

Here's the problem.  When I call "getc" it updates some pointer
somewhere to point to the next char in the stream.  My routines do not
have access to this pointer, so I have to tell them where to start
reading the data.  This works, but it's really annoying to have to
track  the position in the stream which I read sequentially, straight
through, one time.  Is there a way to access the pointer that "getc"
uses, or maybe a library function that I could substitute for my own?

Here's one of the functions that returns data of a specific type to the
calling program:

float getfloat(FILE *fileptr, long offset, int orgin) {
        float floatdata;
        void *ptr;
        int status;
        fseek(fileptr, offset, orgin);
        ptr =&floatdata;
        status = fread(ptr, 4, 1, fileptr); /* read one object of four bytes */
        return floatdata;

--

Andy Fox
--



Tue, 05 Feb 2002 03:00:00 GMT  
 reading from binary files
At this point, you *think* the problem is simply one of gaining access to a
stream pointer, while reading and writing binary data to files.

In fact, the real problem you face is that all your data will be rendered
useless if any of these things happens:

1. Your compiler is updated.
2. Your computer is updated.
3. Your processor is updated.
4. Your OS is updated.
5. Your OS changes.
6. Your processor changes.
7. Your platform changes.
8. Your programming language changes.
9. Any of dozens of similar events takes place.

Just to show you what I mean, here is a line from your post:

<< float getfloat(FILE *fileptr, long offset, int orgin) {

....

ptr =&floatdata;
status = fread(ptr, 4, 1, fileptr); /* read one object of four bytes */
 return floatdata; >>

What? a float variable is *always* four bytes on *every* machine, now and in
the future? And all of them will have the exact same internal formatting as
your current machine's float type?

Please reconsider adding to the vast islands of valuable but unreclaimable
data in binary form, for which so many are paying so much for so little.
Instead, learn how to use text format.

There are ways to read and write binary data portably. Yours is not one of
the ways. It is much easier and safer to use text.

--

Paul Lutus
www.arachnoid.com


<snip>

--



Wed, 06 Feb 2002 03:00:00 GMT  
 reading from binary files
Hi,

I agree that this is not the best way to read and write data portably.
Unfortunately, I have no control over how the data is written - I just
have to read it.

Actually, my program will read these unfortunately formatted data files
and output a comma separated ASCII text file which is readable by just
about anything.

It looks like another subject "Break float into 4 bytes" has cropped up,
which appears to be the same problem in reverse.  Back to the books...

--
Andy Fox - KK7HV - Tucson, AZ USA - DM42 - QRP-L #1286
--



Fri, 08 Feb 2002 03:00:00 GMT  
 reading from binary files


Quote:

> I have written some code to read binary data from a file and convert
> it into the appropriate data type for use in my program.  I have
> written routines that get shorts, longs, floats and doubles.  It
> seemed silly to write a routine for getting chars, since "getc"
> works fine.

> Here's the problem.  When I call "getc" it updates some pointer
> somewhere to point to the next char in the stream.  My routines do not
> have access to this pointer, so I have to tell them where to start
> reading the data.  This works, but it's really annoying to have to
> track  the position in the stream which I read sequentially, straight
> through, one time.  Is there a way to access the pointer that "getc"
> uses, or maybe a library function that I could substitute for my own?

> Here's one of the functions that returns data of a specific type to
> the calling program:

> float getfloat(FILE *fileptr, long offset, int orgin) {
>    float floatdata;
>    void *ptr;
>    int status;
>    fseek(fileptr, offset, orgin);
>         ptr =&floatdata;
>    status = fread(ptr, 4, 1, fileptr);
>                /* read one object of four bytes */
>    return floatdata;

Unless I misunderstand your problem, it looks like you're trying
too hard! You don't need to keep track of your position in the
file or do any fseek()s. When you do a getc(), the Standard I/O
internal pointer is updated to point to the character after the
one you read. If you then do an fread(ptr, 4, 1, fileptr), it
will read the next four characters, and again update the pointer
to point to the character after the last one it read. Using the
various stdio routines to read from the file will simply step
through it sequentially. If you want to skip over a part of the
file, just use fseek(fileptr, skip_count, SEEK_CUR) to skip the
skip_count characters after the last one you read.

Am I missing something?

[ Posted and emailed since the question was a little while ago
  and Andy may no longer be watching for replies. ]

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
--



Wed, 13 Feb 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. reading a binary file back to binary

2. C# Reading a Binary File Nightmare Continues

3. C# Reading a Binary File

4. read a binary file

5. reading a binary file

6. How to read a Binary file?

7. Help Reading from Binary File

8. How can i read from Binary Files ?

9. Reading a binary file

10. Quickly reading a binary file

11. Reading a binary file

12. READING/WRITING BINARY FILES IN C

 

 
Powered by phpBB® Forum Software