Click here to Skip to main content
13,295,243 members (73,488 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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 5:11am
Updated 25-Dec-12 6:01am
H.Brydon 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
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;
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

        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

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.171207.1 | Last Updated 4 Jan 2013
Copyright © CodeProject, 1999-2017
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