fstream operator>> stops at white space

K

khalid302

I need to read a specific number of characters into an std::string
from a file regardless of the characters read.

* fstream operator>> stops when it runs into a white space character.
* The global variant of fstream::getline works with std:string but
does not take a 'width' parameter.
* ifstream::get takes a 'width' parameter but does not work with
std::string.

I simply want to read a specific number of characters and only stop on
EOF. Is there any straight forward way to do it ?
 
K

khalid302

Resize the string first, and rely on the fact that in all (known) current
implementations it has contiguous buffer, and that will be guaranteed in C++0x.

I think std:string automatically accommodates input from operator>>.
am I missing something ?
 
I

Ian Collins

I think std:string automatically accommodates input from operator>>.
am I missing something ?
I think Alf was telling you to do an unformatted read directly into the
string's buffer.
 
K

khalid302

I think Alf was telling you to do an unformatted read directly into the
string's buffer.

What's the API for that, std::string has data() and c_str(), and both
return a const char* which can't/shouldn't be modified.
 
K

khalid302

The only solution I could think of, is to create a custom stream class
over cstdio ... C streams seem to be more mature.
 
J

Juha Nieminen

The only solution I could think of, is to create a custom stream class
over cstdio ... C streams seem to be more mature.

std::string buffer(width, ' ');
inputFile.read(&buffer[0], width);

What's so hard about that?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

fstream File i/o 1
fstream Buffers 26
fstream - write a file 3
TF-IDF 2
Understanding fstream seeking 2
White space and >> 6
eof error using '>>' in fstream 2
[c++] get() and read() function from fstream 3

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top