Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
i have read from a file which are stored in a string array. i want to split string into several parts using stringstream, but this wont store anything and gives empty output for cout :/
                stringstream ssss;
		ssss << arrayy[i];
		getline(ss,line,' ');
		first[i].country = line;
		getline(ss,line,' ');
		first[i].name = line;
but above does not give any output:
and av to use space as delimiter
Posted 25-Dec-12 4:11am
Edited 25-Dec-12 5:01am
v3
Comments
H.Brydon at 3-Jan-13 1:24am
   
You have defined 'ssss' and are using 'ss' which is not defined in this portion of code. What does it look like?
 
Also, is it reasonable that there might be multiple space characters at the beginning of the file?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

follow the below example :
 
#include <iostream>
#include <sstream>
using namespace std;
 
int main () {
 
  int val;
  stringstream ss (stringstream::in | stringstream::out);
 
  ss << "120 42 377 6 5 2000";
 
  for (int n=0; n<6; n++)
  {
    ss >> val;
    cout << val*2 << endl;
  }
 
  return 0;
}</sstream></iostream>
output:
240
84
754
12
10
4000
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

try:
                stringstream ssss;
		ssss << arrayy[i];
		ssss >>	first[i].country ;
		ssss >>	first[i].name ;
but first test that your arrayy[i] contain just 2 words, or at last the 2 fisrt are the correct. (I´m assuming that country and name are members of type std::string or simmilar). This will eat not used spaces
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web03 | 2.8.140926.1 | Last Updated 4 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100