reading binary file

This is a discussion on reading binary file within the C Programming forums, part of the General Programming Boards category; I just want to know if open() can read the binary file with UNIX C....

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    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 support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 09: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, 06:51 PM
  4. Reading data from a binary file
    By John22 in forum C Programming
    Replies: 7
    Last Post: 12-06-2002, 01:00 PM
  5. reading from structs in a binary file
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 12-21-2001, 09:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21