Thread: reading binary file

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    13

    reading binary file

    I just want to know if open() can read the binary file with UNIX C.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    13
    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;
    
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    13
    Originally posted by Salem
    FILE*
    Use fopen() / fread() / fclose()

    As in
    if(!(inFile = fopen("bintest.dat", "rb")))
    i thought read(), write(),close() are for reading binary file

    if I still use fopen(), fread(), i still can read the binary file?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Tree - Reading From and Writing to a File
    By Ctank02 in forum C++ Programming
    Replies: 2
    Last Post: 03-15-2008, 09:22 PM
  2. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  3. Problem reading a delimited file into a binary tree
    By neolyn in forum C++ Programming
    Replies: 10
    Last Post: 12-09-2004, 07:51 PM
  4. Reading data from a binary file
    By John22 in forum C Programming
    Replies: 7
    Last Post: 12-06-2002, 02:00 PM
  5. reading from structs in a binary file
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 12-21-2001, 10:52 AM