473,322 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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 4264
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Armando | last post by:
Hallo ! I habe some error in my programm,because i use <fstream.h>,I want to use <fstream> but i don´t know which fonctions i must modify in my program ? Thanks you for your help. Armando.
6
by: David Briggs | last post by:
I am using MS VC++ 6.0 with MFC I have a simple class: #include <fstream.h> class Data { public: CString WriteStr(); Data();
1
by: Macca | last post by:
Hi, I have been using <fstream.h> in stdafx.h,(i'm using MFC) to output to text files. I have now started to use vectors and when i added #include <vector> using namespace std; to...
4
by: nils | last post by:
Hi all, Recently I started migrating a gcc project to Visual Studio C++ (dotnet). The problem is that I cannot include any iostream header-file: #include <fstream> int main (int argc, char *...
4
by: Falos425 | last post by:
Okay, I've got a driver.cpp, a section.cpp, and a section.h because I've got a 'section' class. I have an #include <fstream> in the section.h and then section.cpp has an #include "section.h" ...
4
by: adamrobillard | last post by:
Hi, I have always used fopen and FILE* to load and save structures to file. I am trying to convert all the older code to use proper C++ calls... the following code works properly but I would...
11
by: Noah Roberts | last post by:
template < typename T > std::istream & operator >(std::istream & in, std::pair<T,T& p) { in >p.first >p.second; return in; } .... std::istream_iterator< std::pair<size_type, size_type
5
by: neowillis | last post by:
code: #include <iostream> #include <fstream> #include <string> using namespace std; int main() {
6
by: puzzlecracker | last post by:
Say we have this structure: Struct Foo{ .... friend ostream& operator << (ostream& s, Foo & m); ..... }; friend ostream& operator << (ostream& s, Foo & m){
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.