473,666 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Stringstream skipping first line

MrPickle
100 New Member
I have a tag like so:
<foo>

and I am trying to extract 'foo' from this.
I'm using a stringstream in a while loop but it seems to fail with the first line, then it works with every other line.

Expand|Select|Wrap|Line Numbers
  1. std::ifstream is("xml.txt");
  2.     if(is.fail())
  3.     {
  4.         return 0;
  5.     }
  6.     std::string str, tag;
  7.     std::stringstream ss;
  8.  
  9.     while(std::getline(is, str))
  10.     {
  11.         tag = "";
  12.         ss.seekg(std::stringstream::beg);
  13.         ss.str(str);
  14.         ss.ignore(256, '<');
  15.         ss >> tag;
  16.         std::cout << ss.str()<< " " << tag << "\n";
  17.     }
Mar 22 '09 #1
3 3549
JosAH
11,448 Recognized Expert MVP
What happens when you swap lines #12 and #13?

kind regards,

Jos
Mar 22 '09 #2
MrPickle
100 New Member
It works O_O

Why does this make a difference?
Mar 22 '09 #3
JosAH
11,448 Recognized Expert MVP
@MrPickle

The first time through the loop your stringstream has no string set to move to the begin of; it sets the stringstream to a fail status. After swapping those two lines it does have a string so it can move to the begin position thereof.

I don't know by head if setting a new string makes the stream moves to its begin position; check it out; if it does you can remove that seekg alltogether.

kind regards,

Jos
Mar 22 '09 #4

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

Similar topics

3
14803
by: Mike Chirico | last post by:
Sorry about the newbie question. What's the best way to convert numbers to strings in C++. The following works, but is it better than using the sprintf() "old C" way of converting? #include <iostream> #include <string> #include <sstream> #include <list>
19
2475
by: Kai-Uwe Bux | last post by:
Hi folks, I have trouble writing a class, derving from stringstream, that collects item and once it's done will write them to std::cout in one go. It works fine except when I use it as a temporary. Here is a tiny test programm: #include <iostream> #include <sstream>
10
2615
by: vigacmoe | last post by:
I was trying to cast some strings to integers with stringstream, then this strange problem poped up. Here is my test code: stringstream conv; string from; int to; from = "1"; conv << from;
0
1424
by: ziyanjoe | last post by:
Hi! I am writing a program for the robot that runs as a daemon on a linux machine. Since debug output cannot be seen on stdout, I want to create an iostream to handle all the output messages. However, I do not want to use a file or output to the clog since I want to achieve the debug message remotely through TCP connection. I've already built a TCP server for transmitting data, all i need is an "endless" stringstream. I tried to code this...
2
7845
by: david.crow | last post by:
Given the following input file: bob 1 2 3 4 5 mary 2 3 4 5 6 7 susan 3 4 5 6 7 8 9 This code snippet does not read it correctly: class Student {
7
3265
by: Gustaf | last post by:
Hi all, Just for fun, I'm working on a script to count the number of lines in source files. Some lines are auto-generated (by the IDE) and shouldn't be counted. The auto-generated part of files start with "Begin VB.Form" and end with "End" (first thing on the line). The "End" keyword may appear inside the auto-generated part, but not at the beginning of the line. I imagine having a flag variable to tell whether you're inside the...
0
1436
by: Jerry Coffin | last post by:
In article <4fae62b0-6858-4e9e-830e-9eecf6691d4a@ 59g2000hsb.googlegroups.com>, friend.blah@googlemail.com says... Each time you read from the file, keep track of the file position after reading. When you read the file the next time, seek to the previously- stored position, then read one line and update the file position.
4
7234
by: BibI | last post by:
Hi there, I just started programming with PERL and am trying to put together my first little data manipulation program. I am working on a MAC with OSX. I have a data file with the following header that has been created on a Windows XP machine: My goal is to get rid of the header and the empty lines to finally have an output file with only the number entries.
3
5897
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 up into individual elements. The elements can be strings, integers or floating point numbers. In the object where I break the line into elements I use a std::stringstream object to do the actual check:
0
8356
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8871
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8551
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 most users, this new feature is actually very convenient. If you want to control the update process,...
1
6198
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5664
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2771
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
2
1776
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.