469,299 Members | 2,078 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,299 developers. It's quick & easy.

fstream operator>> stops at white space

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 ?
Aug 8 '08 #1
8 4017
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 ?
Cheers, & hth.,

- Alf
Aug 8 '08 #2
kh*******@gmail.com wrote:
>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 think Alf was telling you to do an unformatted read directly into the
string's buffer.

--
Ian Collins.
Aug 8 '08 #3
On Aug 9, 1:44*am, Ian Collins <ian-n...@hotmail.comwrote:
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.
Aug 9 '08 #4
The only solution I could think of, is to create a custom stream class
over cstdio ... C streams seem to be more mature.
Aug 9 '08 #5
kh*******@gmail.com wrote:
On Aug 9, 1:44 am, Ian Collins <ian-n...@hotmail.comwrote:
>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.
&string[0]

--
Ian Collins.
Aug 9 '08 #6
kh*******@gmail.com wrote:
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?
Aug 9 '08 #7
On Aug 9, 11:48*am, Juha Nieminen <nos...@thanks.invalidwrote:
* What's so hard about that?
Nothing, just never crossed my mind, thanks :)

Aug 9 '08 #8
On Aug 9, 11:03*am, Ian Collins <ian-n...@hotmail.comwrote:
&string[0]
Yeah, thanks.
Aug 9 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by David Briggs | last post: by
1 post views Thread by Macca | last post: by
4 posts views Thread by nils | last post: by
4 posts views Thread by Falos425 | last post: by
4 posts views Thread by adamrobillard | last post: by
11 posts views Thread by Noah Roberts | last post: by
5 posts views Thread by neowillis | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.