Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Reading binary file with C

Reply
Thread Tools

Reading binary file with C

 
 
acp26b
Guest
Posts: n/a
 
      08-02-2006
I need to read a binary to put into a struct. For some reason the code
will not work. In this section of code i am just looping through the
file to get the number of records so i can then build the correct size
array. Nothing was happening so i used the variable test and found out
that read is returning -1. Any ideas?

char *inFileName = NULL;
void *input;
int fdi;
int sizeOfData = sizeof(bin_record);

fdi = open(inFileName, O_RDONLY);
if (fdi == -1){
printf("ERROR: UNABLE TO OPEN DATA FILE\n");
exit (-1);
}

test = read(fdi, input, sizeOfData);
//while( read(fdi, ptr, sizeOfData) > 0){
// recordCount++;
//}

printf("%d \n", test);
close (inFileName);

 
Reply With Quote
 
 
 
 
Bill Pursell
Guest
Posts: n/a
 
      08-02-2006
acp26b wrote:
> I need to read a binary to put into a struct. For some reason the code
> will not work. In this section of code i am just looping through the
> file to get the number of records so i can then build the correct size
> array. Nothing was happening so i used the variable test and found out
> that read is returning -1. Any ideas?
>
> char *inFileName = NULL;
> void *input;
> int fdi;
> int sizeOfData = sizeof(bin_record);
>
> fdi = open(inFileName, O_RDONLY);
> if (fdi == -1){
> printf("ERROR: UNABLE TO OPEN DATA FILE\n");
> exit (-1);
> }
>
> test = read(fdi, input, sizeOfData);
> //while( read(fdi, ptr, sizeOfData) > 0){
> // recordCount++;
> //}
>
> printf("%d \n", test);
> close (inFileName);


Replace the printf("%d\n", test); with perror(NULL). It may
tell you the reason. For that matter, you might want to
replace printf("ERROR: UNABLE TO OPEN DATA FILE\n");
with perror(inFileName); It will give you a far more useful
diagnostic.

 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      08-02-2006
"acp26b" <(E-Mail Removed)> writes:
> I need to read a binary to put into a struct. For some reason the code
> will not work. In this section of code i am just looping through the
> file to get the number of records so i can then build the correct size
> array. Nothing was happening so i used the variable test and found out
> that read is returning -1. Any ideas?
>
> char *inFileName = NULL;
> void *input;
> int fdi;
> int sizeOfData = sizeof(bin_record);
>
> fdi = open(inFileName, O_RDONLY);
> if (fdi == -1){
> printf("ERROR: UNABLE TO OPEN DATA FILE\n");
> exit (-1);
> }
>
> test = read(fdi, input, sizeOfData);
> //while( read(fdi, ptr, sizeOfData) > 0){
> // recordCount++;
> //}
>
> printf("%d \n", test);
> close (inFileName);


open, read, and close are not standard C functions. The corresponding
standard functions are fopen, fread, and fclose. If you can modify
your program to use the standard functions, we'll be glad to help;
otherise, try (probably) comp.unix.programmer.

I note that the first argument to open() is a null pointer; you
initialize inFileName to NULL, and you don't modify it -- at least in
the code you've shown us.

If you do set inFileName to some non-null value in code you haven't
shown us, then you're asking us to guess what's wrong with your actual
code based on some *other* code that you've posted. We're not
psychic. Post a complete compilable program.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
acp26b
Guest
Posts: n/a
 
      08-02-2006
i figured out the problem i was missing the & sign infront of the void
pointer.

About posting compilable code, the reason i did not do this was because
to post full compilable code i would have had to post multiple files, i
dont use fourms much and thought it would be better to only post the
few lines that were causing the problem i thought it was understood
that since it was opening the file and the problem was with the read
that the file name was there. Sorry about that I will know better next
time...


Keith Thompson wrote:
> "acp26b" <(E-Mail Removed)> writes:
> > I need to read a binary to put into a struct. For some reason the code
> > will not work. In this section of code i am just looping through the
> > file to get the number of records so i can then build the correct size
> > array. Nothing was happening so i used the variable test and found out
> > that read is returning -1. Any ideas?
> >
> > char *inFileName = NULL;
> > void *input;
> > int fdi;
> > int sizeOfData = sizeof(bin_record);
> >
> > fdi = open(inFileName, O_RDONLY);
> > if (fdi == -1){
> > printf("ERROR: UNABLE TO OPEN DATA FILE\n");
> > exit (-1);
> > }
> >
> > test = read(fdi, input, sizeOfData);
> > //while( read(fdi, ptr, sizeOfData) > 0){
> > // recordCount++;
> > //}
> >
> > printf("%d \n", test);
> > close (inFileName);

>
> open, read, and close are not standard C functions. The corresponding
> standard functions are fopen, fread, and fclose. If you can modify
> your program to use the standard functions, we'll be glad to help;
> otherise, try (probably) comp.unix.programmer.
>
> I note that the first argument to open() is a null pointer; you
> initialize inFileName to NULL, and you don't modify it -- at least in
> the code you've shown us.
>
> If you do set inFileName to some non-null value in code you haven't
> shown us, then you're asking us to guess what's wrong with your actual
> code based on some *other* code that you've posted. We're not
> psychic. Post a complete compilable program.
>
> --
> Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
> San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
> We must do something. This is something. Therefore, we must do this.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie: working with binary files/extract png from a binary file Jim Ruby 6 12-24-2013 08:09 AM
[Python3] Reading a binary file and wrtiting the bytes verbatim in an utf-8 file Python 6 04-25-2010 07:46 AM
writing binary file (ios::binary) Ron Eggler C++ 9 04-28-2008 08:20 AM
Reading Binary Content from a File and Filtering it Neo ASP .Net 0 06-29-2005 05:05 PM
reading binary file. El Durango Java 1 12-08-2004 07:24 AM



Advertisments