0

So Ive got a struct of

struct records {
    short link; 
    double gate; 
    unsigned char bar;
    int rest; 
    char rink; 
};

And now I want to read binary inputs and print them out using fread(). I'm just having trouble figuring out what exactly to do.

So I've only got

int main(int argc, char* argv[]){
}

So first off, how do you open a binary file? All the examples I've seen online only use main() and when using fopen() they always specify the input. How to use the command line arguments to specify which file to open?

And then, how do I read those files into the struct I've created and print them out?

Any help appreciated, thank you so much.

1
  • Hint 1: You forgot the return 0; statement to return the result of main(). Hint 2: Once you made the changes of hint 1, set a breakpoint at the return statement and inspect the contents of argc and argv while supplying different command line arguments to the program. Commented Aug 12, 2017 at 4:57

1 Answer 1

2

Provide the name of the binary file as argument when you run the program.

argv[0] will be the name of the program itself and the name of the file in argv[1].

As Jonathan mentioned in the comment, you must ensure that argv[1] actually exists before you access it or it will cause error.

Something like

if(argc<2)
{
    printf("\nError");
    return -1;
}

should take care of that.

Then do

FILE *fin=fopen(argv[1], "rb");

and then use fread() to read from the file.

The second argument of fopen() is the mode in which file is opened. r and b in "rb" means read and binary respectively.

As for fread(), first create a variable, say a of type struct records.

Then use

fread(&a, sizeof(a), 1, fin);

Here we pass address of a so that the read data will be stored there. sizeof(a) says the size of each block read by fread() and the 1 is the number of blocks that should be read.

fread() will return the number of blocks that were read successfully which in our case is 1. If the number of blocks that we asked to be read and the number of blocks that were actually read are not same, either the file ended or some error occurred.

Afterwards we can access each element of a.

https://www.tutorialspoint.com/c_standard_library/c_function_fread.htm http://pubs.opengroup.org/onlinepubs/009695399/functions/fread.html

Sign up to request clarification or add additional context in comments.

9 Comments

Thanks so much. Ive done the error handling, like the argc<2 and more like if(fin==null). Could you please elaborate more on fread()?
@AlonzoRobbe I added some more content. Hope it helps.
Thanks again. I have a couple questions. First, does the 1 in the fread refer to how many it reads from the struct, or just how many structs. So for my struct, would it still be 1 or 5? Second, to access each element of a and print it, would I just do printf("%d", a[0]) and so on for a[1]? Lastly, we've been told to "Read each field separately with its own fread call." Does this differ from what you have said?
@AlonzoRobbe The 1 refers to the number of blocks which in this case is a single struct records. A single block will have all the 5 members. So it's 1 and not 5. To access each element of a, you will have to go for the manual way ie, printf("%d,%lf,...", a.link, a.gate, ...). Reading each field separately with its own fread() and what I suggested are unlikely to be the same. And I don't even know if it can be done.
@AlonzoRobbe I guess by each field you mean the link, gate, etc
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.