I just want to know if open() can read the binary file with UNIX C.
I just want to know if open() can read the binary file with UNIX C.
Yes
Well technically, open just allows access to files, you use read() to actually read from the file
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
then is my sytax wrong?
I have a binary file named bintest.dat
Code:int readBinary(struct test *b) { FILE *inFile; if(!(inFile = open("bintest.dat", "r"))) return 1; read((struct test *)b,sizeof(struct test),1,inFile); close(inFile); return 0; }
FILE*
Use fopen() / fread() / fclose()
As in
if(!(inFile = fopen("bintest.dat", "rb")))
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
i thought read(), write(),close() are for reading binary fileOriginally posted by Salem
FILE*
Use fopen() / fread() / fclose()
As in
if(!(inFile = fopen("bintest.dat", "rb")))
if I still use fopen(), fread(), i still can read the binary file?
if(!(inFile = fopen("bintest.dat", "rb")))
It's the 'b' which makes it binary
> i thought read(), write(),close() are for reading binary file
No, these are the POSIX functions for accessing files (fopen() etc are ANSI-C functions)
Like
int fd = open( "bintest.dat", O_RDONLY );
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.