473,561 Members | 3,128 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::getlin e 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 4279
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
implementation s 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.co mwrote:
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.co mwrote:
>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.co mwrote:
&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
3192
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
3205
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
2126
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 stdafx.h, I found that to compile i had to change <fstream.h> to <fstream> to get it to compile.
4
4833
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 * argv) { printf("hello world"); return 0;
4
6966
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" Trouble is, one of the class functions is supposed to (homework) take a string as a parameter, and use it as the filename. So in the function, I've...
4
7846
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 like to know if I am using the fstream methods properly. (as long as I am cleaning up I might as well do it right). By the way, this is just a bogus...
11
2047
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
4156
by: neowillis | last post by:
code: #include <iostream> #include <fstream> #include <string> using namespace std; int main() {
6
1915
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
7568
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7858
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8085
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7614
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7928
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6206
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5469
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5185
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2066
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.