473,507 Members | 9,962 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String stream repeating first word?

MrPickle
100 New Member
I am tokenizing a line using a stringstream and a vector but the stringstream seems to be repeating the first word, here's my code:

Expand|Select|Wrap|Line Numbers
  1. std::stringstream ss;
  2. std::vector<std::string> LineVec;
  3. std::string Line, Word;
  4. while(std::getline(File, Line)) {
  5.    if(Line.empty()) { continue; }
  6.    ss.str(Line);
  7.    ss.seekg(std::ios::beg);
  8.    while(ss >> Word) { LineVec.push_back(Word); }
  9.  
  10.    //Un related stuff here
  11.  
  12.    LineVec.clear();
  13. }
Here's an example input-output
Input: Hello, My name's MrPickle
Output: Hello, My name's MrPickle Hello,
Oct 11 '08 #1
4 2389
boxfish
469 Recognized Expert Contributor
This line
while(ss >> Word) { LineVec.push_back(Word); }
reads words from ss until ss contains no more words- and then ss fails and won't read anything more. Try calling ss.clear() after you do this.
Hope this helps.
Oct 11 '08 #2
oler1s
671 Recognized Expert Contributor
Show us a complete, compileable, snippet of code that we can directly run (for example using std::cin and not a file) to see the problem for ourselves. Because I can construct based on what you did, and have it work just fine.
Oct 12 '08 #3
MrPickle
100 New Member
Sorry, I had made it output the first word later on. I had forgotten about it and missed it when looking for the reason why the first word was being repeated.

Sorry & thanks.
Oct 12 '08 #4
boxfish
469 Recognized Expert Contributor
Because I can construct based on what you did, and have it work just fine.
I did that, and it worked fine for the first line, but after that it stopped working because ss had failed.
Oct 12 '08 #5

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

Similar topics

16
4317
by: Steve | last post by:
Hi Guys, I have a string which contains data elements separated by spaces. I also have a function which returns the number of characters from the beginning of the string for a given number of...
20
11272
by: hagai26 | last post by:
I am looking for the best and efficient way to replace the first word in a str, like this: "aa to become" -> "/aa/ to become" I know I can use spilt and than join them but I can also use regular...
2
1220
by: Mullin Yu | last post by:
as subject, what's the normal way to implement? the stream is binary as it may be come from word, images or other objects?
14
7658
by: msnews.microsoft.com | last post by:
How can I encrypt and decrypt string?
8
5198
by: KRoy | last post by:
I have a password stored in the Registry encrypted using System.Security.Cryptography DES Algorithm. I supplied it a password and a Initialization Vector. I am trying to decrypt it using the...
14
2897
by: nishit.gupta | last post by:
Is their any single fuction available in C++ that can determine that a string contains a numeric value. The value cabn be in hex, int, float. i.e. "1256" , "123.566" , "0xffff" , It can also...
3
6883
by: bagelman | last post by:
Hi, I want to find repeating words in a long string with Regular Expressions. I tried to write a regular expression but it didn't work. "\b(?<word>\w+)\s+(\k<word>)\b" This RegEx finds...
6
5685
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
2490
by: yogi_bear_79 | last post by:
I'm sure I have a few things wrong here. But I am stuck on how to do a recurring search. Also my statement cin >quote; acts weird. If I enter more than one word it blows right past cin >findMe;...
0
7221
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7109
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
7313
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,...
1
5039
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...
0
4702
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
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1537
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 ...
1
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
411
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...

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.