473,466 Members | 1,360 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Behavior of C++ input streams at the end of the stream

16 New Member
Hello all,

I have a question that concerns how C++ input streams (istream, ifstream, istringstream, etc.) behave using the extraction (>>) operator when at the end of a stream's contents.

For instance, I have an input file which is just a one-line string: "do_stuff 20060101 " (yes, that is a trailing space)

I'm using a parse function that attempts to extract a string ("do_stuff") and a long ("20060101") by using some code roughly equivalent to:

Expand|Select|Wrap|Line Numbers
  1. while(myStream.good() && !myStream.eof()) {
  2.     string nextName;
  3.     long   nextID;
  4.     myStream >> nextName >> nextID;
  5.     //pass on this data, rinse, and repeat
  6.     //...
  7. }
  8.  
This seems to work fine until the end, where the process extracts an additional pair <"", whatever-the-last-number-was>. In the example of the above-mentioned file, I parse two string-long pairs instead of one:

<do_stuff, 20060101> < , 20060101>

How do streams handle whitespace at their end? Do they read an empty string after that? What is the explanation for the behavior I'm seeing?

Also, I'm fully aware that I'm a total newb with C++, yet I know this method is incredibly flawed and lacking robustness. I know full well what I need to do; I'm just not sure how to do that in the best way using built-in stream functions. For instance what are the proper ways to test that:
a) a stream still contains more tokens (or better yet, non-empty tokens)
b) a given extraction succeeded, and if not, why?

Not that it should matter (I'm only using standard C++ language/lib features), but I'm writing in MacOSX.4, using g++ v4.0.1.

Note also that this is not a homework assignment, nor am I interested primarily in a solution to my bug (though I do want to fix it). I want to understand how this feature works and how it is used properly. Any caveats/nuances/tips/tricks that anybody would like to share would be greatly appreciated!

Thanks so much,
-losingToTheDevil

P.S. The entire method code is posted below. If more code is needed to answer my questions, please let me know.

Expand|Select|Wrap|Line Numbers
  1. //re-construct queue from file stream
  2. TodoQueue::TodoQueue(ifstream& aInstream)
  3. {
  4.     //common initializer for all constructors
  5.     init();
  6.  
  7.     ListNode* current = head;
  8.     while(aInstream.good() && !aInstream.eof()) {
  9.         string nextJobName;
  10.         long   nextJobDeadline;
  11.  
  12.         //read the next two tokens from the file
  13.         aInstream >> nextJobName >> nextJobDeadline;
  14.  
  15.         //debugging code REMOVE THIS
  16.         cout << "Next job: " << nextJobName << ", " << nextJobDeadline;
  17.  
  18.         current->pNext = new ListNode(nextJobName, nextJobDeadline);
  19.  
  20.         //job queue has one more job in it
  21.         size++;
  22.  
  23.         current = current->pNext;
  24.     }
  25. }
  26.  
Apr 27 '07 #1
1 1675
mvbrevern
5 New Member
Hello all,

I have a question that concerns how C++ input streams (istream, ifstream, istringstream, etc.) behave using the extraction (>>) operator when at the end of a stream's contents.

For instance, I have an input file which is just a one-line string: "do_stuff 20060101 " (yes, that is a trailing space)

I'm using a parse function that attempts to extract a string ("do_stuff") and a long ("20060101") by using some code roughly equivalent to:

Expand|Select|Wrap|Line Numbers
  1. while(myStream.good() && !myStream.eof()) {
  2.     string nextName;
  3.     long   nextID;
  4.     myStream >> nextName >> nextID;
  5.     //pass on this data, rinse, and repeat
  6.     //...
  7. }
  8.  
This seems to work fine until the end, where the process extracts an additional pair <"", whatever-the-last-number-was>. In the example of the above-mentioned file, I parse two string-long pairs instead of one:

<do_stuff, 20060101> < , 20060101>

How do streams handle whitespace at their end? Do they read an empty string after that? What is the explanation for the behavior I'm seeing?

Also, I'm fully aware that I'm a total newb with C++, yet I know this method is incredibly flawed and lacking robustness. I know full well what I need to do; I'm just not sure how to do that in the best way using built-in stream functions. For instance what are the proper ways to test that:
a) a stream still contains more tokens (or better yet, non-empty tokens)
b) a given extraction succeeded, and if not, why?

Not that it should matter (I'm only using standard C++ language/lib features), but I'm writing in MacOSX.4, using g++ v4.0.1.

Note also that this is not a homework assignment, nor am I interested primarily in a solution to my bug (though I do want to fix it). I want to understand how this feature works and how it is used properly. Any caveats/nuances/tips/tricks that anybody would like to share would be greatly appreciated!

Thanks so much,
-losingToTheDevil

P.S. The entire method code is posted below. If more code is needed to answer my questions, please let me know.

Expand|Select|Wrap|Line Numbers
  1. //re-construct queue from file stream
  2. TodoQueue::TodoQueue(ifstream& aInstream)
  3. {
  4.     //common initializer for all constructors
  5.     init();
  6.  
  7.     ListNode* current = head;
  8.     while(aInstream.good() && !aInstream.eof()) {
  9.         string nextJobName;
  10.         long   nextJobDeadline;
  11.  
  12.         //read the next two tokens from the file
  13.         aInstream >> nextJobName >> nextJobDeadline;
  14.  
  15.         //debugging code REMOVE THIS
  16.         cout << "Next job: " << nextJobName << ", " << nextJobDeadline;
  17.  
  18.         current->pNext = new ListNode(nextJobName, nextJobDeadline);
  19.  
  20.         //job queue has one more job in it
  21.         size++;
  22.  
  23.         current = current->pNext;
  24.     }
  25. }
  26.  
you may not work with a simple while loop as in your code.
In C++ (as in C) the eof flag is only raised after your first unsuccesful
reading. That is, eof will not return true after you have read the last
line.
Apr 27 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Ronald Legere | last post by:
The new itertools stuff is pretty cool. One thing that bothers me though is that there seems to be no way to copy an iterator. How does one work around this? Is there a trick that I am missing? ...
21
by: Jason Heyes | last post by:
I want to allow objects of my class to be read from an input stream. I am having trouble with the implementation. Here are the different approaches I have tried: // Version 1.0 - Default...
4
by: Oz | last post by:
This is long. Bear with me, as I will really go through all the convoluted stuff that shows there is a problem with streams (at least when used to redirect stdout). The basic idea is that my...
7
by: Generic Usenet Account | last post by:
I am trying to set up a delimiter character string for the input stream such that the delimiter string is "skipped over" in the input stream. Can someone suggest how to do this with some sample...
9
by: kernelxu | last post by:
hi,everybody. I calling function setbuf() to change the characteristic of standsrd input buffer. some fragment of the progrem is: (DEV-C++2.9.9.2) #include <stdio.h> #include <stdlib.h> int...
2
by: bonk | last post by:
Hello how do I connect streams in c# ? Imagine the followung scenario: I have a StreamWriter that writes Text to a Stream. How can I tell that Stream to pass that Data to another Stream...
1
by: Chris | last post by:
I'm reading up on streams and I have two articles that seem to conflict with each other. One article describes streams and lists a few of the major ones (FileStream, Memory Stream, Network...
28
by: v4vijayakumar | last post by:
#include <string> #include <iostream> using namespace std; int main() { string str; str.resize(5); str = 't';
27
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
I have a fully-portable C program (or at least I think I do). It works fine on Windows, but malfunctions on Linux. I suspect that there's something I don't know about the standard input stream...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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...
1
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...
0
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,...
0
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.