473,385 Members | 1,325 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,385 software developers and data experts.

Using stringstream in C++

9
I have an input file that has lines containing integers, characters and strings that has to be parsed to extract information from it. I am using an istringstream to do this but am having problems getting it to work.

The lines can look like:

2 : 2 5 5 8
2 = 2 * 2
show 2

I already have a part working where the char colon contains ':' and poly and location are integers

if (iss >> poly >> colon)
{
location = poly;

while(iss >> coeff >> exp)
{
//do something
}
}

So this correctly extracts the information from a line in the form of the first line from my example lines.

In the following example, show is declared as a string containing "show" but it does not work.

Expand|Select|Wrap|Line Numbers
  1.  if(iss >> show >> poly)
  2.    {      
  3.     locationOfPoly = poly;
  4.     cout <<"Poly[" << locationOfPoly << "] = " << Poly[locationOfPoly] << "\n";
  5.    }
  6.  
Can an istringstream be used in this way? Or, in other words is it possible to check if the istringstream reads something in particular as I am trying to do above? Thanks.
Oct 3 '07 #1
1 2397
weaknessforcats
9,208 Expert Mod 8TB
The >> operator is for formatted input. That is, you already know the being extracted.

If you don't and extract an int into a char, for example, you will get the stream failbit set and the >> operator just returns without doing anything - and laving the bad data in the stream. All subsequent >> calls test the failbit first, and of set, they return without doing any thing.

If this is text input, you may want to just get trhe file a byte at a time so you can interogate wahet you got and make a decision at that time.
Nov 22 '07 #2

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

Similar topics

5
by: cherico | last post by:
I'd like to read stings from cin and put them in stringstream. I use a string object as an intermediate "container" to store data from cin and then put them in stringstream. stringstream ss ;...
4
by: Vijai Kalyan | last post by:
I wrote the following function as a curiosity: template<typename SourceType, typename DestinationType> DestinationType NumericCast(const SourceType& value) { std::wstringstream strbuf; strbuf...
1
by: Edward Diener | last post by:
If you run this simple console program after building it on VC++ .NET 2003, you will see the error message, "Error getting initial stream position", from the attempt to use the...
2
by: avidamani | last post by:
Hi, I have a c++ application compiled using Compaq C++ V6.5-014 for Compaq Tru64 UNIX V5.1A (Rev. 1885) Compiler Driver V6.5-014 (cxx) cxx Driver The problem is that the application is...
9
by: martinezfive | last post by:
Hi, I feel no doubt some documentation contains my answer, so bare with me. Given the following: #inclde <stdio.h> #include <sstream> void f() { std::stringstream a("Hello World!\n");
5
by: Gary Wessle | last post by:
Hi how can I clear a stringstream? ss.clear(); or ss.flush(); thanks
7
by: Ziyan | last post by:
I am writing a C/C++ program that runs in background (Linux). Therefore, normally no output would be written into standard output. However, sometimes I want to have debug message collected and sent...
4
by: clb | last post by:
I am trying to use stringstreams and my book doesn't cover the included methods. For example, if I init a istringstream on a string and suck all the data out, then put more stuff into the string,...
6
by: arnuld | last post by:
This works fine, I welcome any views/advices/coding-practices :) /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a program to store each line from a file into a *...
3
by: Rune Allnor | last post by:
Hi all. I am trying to use std::stringstream to validate input from a file. The strategy is to read a line from the file into a std::string and feed this std::string to an object which breaks it...
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...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.