File handling in C++

From: John Ruiz (toenails777_at_hotmail.com)
Date: 02/29/04


Date: Sun, 29 Feb 2004 03:25:36 -0800

Hello everyone. I find it amazing how in C++ that reading in files still
more or less requries usage of char * type. I know there's getline to use
std::string, but the basic_stream::get methods don't work with std::string.
I would like to create a function perhaps something like:

    void read(std::string &s, int numBytes);

where the user passes in s to have numBytes of data read from a file into s.
Well, this would require me to do a thing like allocate memory of char bytes
and then do a copy over to the string. So, for this reason, I find myself
sticking to doing the C thing and using FILE pointers and all because to me,
it seems like a waste to read into a char array, then copy over to
std::string, so to avoid that copy, I just do the C thing.

So, am I unaware of a way around this problem? Thanks in advance.

John



Relevant Pages

  • Re: Reading a Binary File....
    ... I have a problem in reading a binary file. ... int acountnum; ... char phone; ...
    (comp.lang.c)
  • [9fans] Questions on notes
    ... top of that there always seems to be a race condition between somebody reading on /proc/n/note ... Speaking of constant polling: the following hangs 9vx for good on my system: ... void door_bell(void* dummy, char* note) ... int main ...
    (comp.os.plan9)
  • Re: "has been reading" and "was reading"
    ... Char se temas pri la lasta, ... > - has been reading: legas ... inter la signifo de 'Father had been reading', ...
    (soc.culture.esperanto)
  • Re: What is the gain of "inline"
    ... int bla{ ... char p; ... /* do lots of things on p - reading that many characters from a file ...
    (comp.lang.c)
  • Re: What is the gain of "inline"
    ... char p; ... /* do lots of things on p - reading that many characters from a file ... and hashing them, say */ ...
    (comp.lang.c)