- Reading binary data
- Opening and closing files
- Printing various data types using printf.
- Using a simple command-line parameter.
- reading a binary file with passed parameter
- int main(int argc, char *argv[])
- after compile file, call the execution
- ex: ./stage2 input-2.bin
- argc must be 2, if not, print error by fprint(stderr, "message")
- argv[0]: stage2
- argv[1]: input-2.bin
- read the file and set it to the file pointer
- FILE *fp = fopen(filename, "rb"); //r: read, b: binary
- pass filename by argv[1]
- check fp is null or not
- if not, print out error by fprinpt
- How to read binary data from start to end
- function: fread - http://www.cplusplus.com/reference/cstdio/fread/
- while( fread((void *)&initStruct.geese, sizeof(initStruct.geese), 1, fp) != 0 )
- stop when eof
- Print long integer - most important
- use "%ld"
0 comments:
Post a Comment