Reading into & from a buffer

This is a discussion on Reading into & from a buffer within the C++ Programming forums, part of the General Programming Boards category; hi, wondering if someone could help on best way to read a file into a buffer in such a way ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Location
    Australia
    Posts
    55

    Reading into & from a buffer

    hi,
    wondering if someone could help on best way to read a file into a buffer in such a way as to be able to set the seek() on the buffer (or file position), and then read from that buffer based on the buffer's (file) position? Instead of using &read() function for every call.

    Example;

    Firstly read a whole file into a buffer.
    And then using the buffer, being able to do something like this;
    Code:
    	offset = 0;
    	buffer.seekp(offset);
    	buffer.seekg(offset);
    	buffer.read((char*)&cChar,sizeof(cChar));
    	cout<<"cChar: "<<cChar<<endl;

    I'm still relatively new to file i/o, so any help would be great.
    Thanks!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I'm sure there's a reason you're not using array indexing, but you'll have to tell me what it is. Are you trying to write something that works on both a stream and a block of memory?

  3. #3
    Registered User
    Join Date
    Apr 2008
    Location
    Australia
    Posts
    55
    array indexing, for file i/o's? Don't know - as I said, I'm still trying to grasp some file i/o aspects.
    I want to be able to read/write but not directly to file all the time, since I'm guessing that requires disc reading/writing each time an operation is done (yes?). Hence why I want to use a buffer, but I'm not sure the best way to go about doing that. But I'd like to be able to set the file position still on the buffer itself & read/write to/from the buffer and then to/from the file.


    I'm just not sure how to approach it (syntax), many of the articles I've read aren't very helpful/descriptive, so if there's some (other) way in doing something like this, I'm open to suggestions.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    But a buffer isn't file i/o, unless we mean radically different things by those terms from each other. You would read a chunk of a file into a buffer. But a buffer is just an array of bytes/characters. (Or you can write to a buffer, but again that's just a chunk of memory as an array, not a file in itself.)

  5. #5
    Registered User
    Join Date
    Apr 2008
    Location
    Australia
    Posts
    55
    hhmm...ok. Makes sense.
    So essentially, once a part/whole file has been read-in to some sort of array, then it simply gets treated as such; an array. So you can't really perform file i/o functions on the array (directly like you would a file).


    So, what's the best type to read-in data that'll keep a file 'as is', byte for byte? char*/string/both?


    Thanks for the clarification. All this time I was thinking that the buffer is some sort of file i/o mechanism .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multithreading (flag stopping a thread, ring buffer) volatile
    By ShwangShwing in forum C Programming
    Replies: 3
    Last Post: 05-19-2009, 07:27 AM
  2. UDP socket partial reading
    By mynickmynick in forum Networking/Device Communication
    Replies: 0
    Last Post: 03-25-2009, 11:43 AM
  3. Print out a buffer
    By SwarfEye in forum C Programming
    Replies: 4
    Last Post: 09-08-2006, 09:32 AM
  4. Reading data from buffer
    By timmer in forum C++ Programming
    Replies: 2
    Last Post: 04-25-2005, 07:03 AM
  5. reading command line arguments into buffer
    By jpp1cd in forum C Programming
    Replies: 2
    Last Post: 12-12-2002, 07:08 PM

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