irritating corner-case c++ question
Feb. 29th, 2008 03:19 amIs there any portable way to read [as in the system call] from a file directly into a C++ standard string? One would like to do something like
str.reserve(filesize); got = ::read(fd, str.data(), str.capacity()); str.enlarge_to_cover(got);
(error handling and loop until EOF omitted for clarity) but I can't actually find a method that does enlarge_to_cover(). Also, data() returns a read-only pointer.
... why am I still awake?
no subject
Date: 2008-02-29 02:42 pm (UTC)no subject
Date: 2008-02-29 05:26 pm (UTC)no subject
Date: 2008-02-29 06:11 pm (UTC)FD = ::open (fileName->c_str(), O_RDONLY);
size = statInfo.st_size;
file = (char *)malloc (size);
int bytes_read = ::read (FD, file, size);
would save the problem of having to redo the size. Replace malloc
with str.reserve...
should work i think.
no subject
Date: 2008-02-29 06:39 pm (UTC)Plus, data() and c_str() are read-only. Gah.
(I suppose I could use the method whose name I forget and then overwrite with read() but that's not going to be any faster than using a scratch char[] and copying.)
no subject
Date: 2008-02-29 06:53 pm (UTC)Bah.
no subject
Date: 2008-03-05 08:01 pm (UTC)