Binary files

This is a discussion on Binary files within the C++ Programming forums, part of the General Programming Boards category; Since,binary file do not store special character(ascii no 26) for end of file.Then is it safe to use Code: while(fileobj.peek!=EOF) ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    113

    Binary files

    Since,binary file do not store special character(ascii no 26) for end of file.Then is it safe to use

    Code:
    while(fileobj.peek!=EOF)
    If not then what should I use?

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I'm not exactly clear as to what you're asking. If you want to read in data until the end of the file is reached, you could use:
    Code:
       while (fileobj.read(array, sizeof(array))
       {
          //Do something with array
       }
    Or you could read one char at a time:
    Code:
       while (fileobj.get(ch))
       {
          //Do something with ch
       }

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > If not then what should I use?
    You examine the return status of file reading functions to determine whether you reached end of file, or some other error.
    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.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    113
    I just want to ask is it correct to use following loop termination in case of a binary file
    Code:
    ifstream obj;
    obj.open("file.dat",ios::binary);
    while(obj.peek()!=EOF)
    {
    //Do some operation on each record
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 10:46 AM
  2. send/recv binary files using sockets in C++
    By dafatdude in forum Networking/Device Communication
    Replies: 14
    Last Post: 07-25-2004, 11:00 AM
  3. MFC: CStrings & binary files question(s)
    By BrianK in forum Windows Programming
    Replies: 0
    Last Post: 06-24-2004, 05:41 PM
  4. Binary files
    By Brian in forum C Programming
    Replies: 2
    Last Post: 02-18-2002, 12:13 PM
  5. storing string objects to binary files
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-06-2001, 11:33 PM

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