Thread: Reading a file in binary question

  1. #1
    System-7
    Join Date
    Nov 2005
    Posts
    65

    Reading a file in binary question

    When reading a file in binary is there any way to have it stop at a delimiter such as '\n'. From what I've found you use read() but that only allows for the character array and the size of the array no delimiter. I was using getline but then when i switched to binary, that no longer worked so I assumed that since its binary, getline doesnt really get along with it.

    I need to read it in binary and I need a way to search for a string within it. That's why I need to read line by line.

    Any help that you can give is appreciated

    Thanks,

    ...Dan

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Well fread() can read a file in binary because of how it works. It reads a file one item at a time, each item being of the same size. The man page gives a lot better of an explanation than me.
    From the fread man page:

    #include <stdio.h>

    size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);

    DESCRIPTION:
    The function fread reads nmemb elements of data, each size bytes long,
    from the stream pointed to by stream, storing them at the location
    given by ptr.
    Not exactly a test for newline, but you at least you are reading the file into a buffer responsibly without a significant risk of overflowing it. It's the fgets() of binary files. Please read thge manual.
    Last edited by whiteflags; 05-16-2006 at 03:56 PM.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I need to read it in binary and I need a way to search for a string within it.
    Why do you need to read the file in binary?

    If you need to search for '\n', then you could read a byte at a time, but I doubt that is the best option.

    I don't see any reason to use fread in this C++ program.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    >I don't see any reason to use fread in this C++ program.
    It's really a C solution to a C++ problem. At least it does work, but I posted it in the wrong place. I hope someone forgives me for that...

    I don't really see why file.get(char *buffer, streamsize num, char delim) wouldn't work either, now that I think of it; it reads byte-by-byte too, so it can work in binary mode.
    Last edited by whiteflags; 05-17-2006 at 11:44 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with reading the end of a binary file
    By firyace in forum C Programming
    Replies: 4
    Last Post: 07-09-2007, 04:44 PM
  2. Question regarding reading data from file into arrays
    By vutek0328 in forum C Programming
    Replies: 3
    Last Post: 05-29-2007, 09:20 AM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. Problems in reading binary file
    By serena in forum C Programming
    Replies: 3
    Last Post: 04-14-2005, 03:54 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM