Reading binary file with C

A

acp26b

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);
 
B

Bill Pursell

acp26b said:
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.
 
K

Keith Thompson

acp26b said:
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.
 
A

acp26b

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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top