Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8571291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:44:59+00:00 2026-06-11T18:44:59+00:00

I am trying to write program for parsing and processing text file. After not

  • 0

I am trying to write program for parsing and processing text file.
After not being successful with implementing sscanf i decided to try stringstream.

I have vector of strings containing data separated by spaces, like:

some_string another_string yet_another_string VARIABLE_STRING_NO_1
next_string

I wrote code and expected result would be:

Counter: 4
Variable number 1 : VARIABLE_STRING_NO_1
Variable number 2 : VARIABLE_STRING_NO_2
Variable number 3 : VARIABLE_STRING_NO_3
Variable number 4 : VARIABLE_STRING_NO_4

but instead i get:

Counter: 4
Variable number 1 : VARIABLE_STRING_NO_1
Variable number 2 : VARIABLE_STRING_NO_1
Variable number 3 : VARIABLE_STRING_NO_1
Variable number 4 : VARIABLE_STRING_NO_1

Can anyone push me in right direction please? (eg. use other container instead of vector, change method to… etc)

Also what if VARIABLE_STRING contains 2 sub-strings with space inbetween? That is possible in my data.

Sample code:

#include <iostream>
#include <sstream>
#include <vector>
using namespace std;

int main()
{
    vector<string> vectorOfLines, vectorOfData;

    vectorOfLines.push_back("some_string another_string yet_another_string VARIABLE_STRING_NO_1 next_string");
    vectorOfLines.push_back("some_string another_string yet_another_string VARIABLE_STRING_NO_2 next_string");
    vectorOfLines.push_back("some_string another_string yet_another_string VARIABLE_STRING_NO_3 next_string");
    vectorOfLines.push_back("some_string another_string yet_another_string VARIABLE_STRING_NO_4 next_string");

    string data = "", trash = "";
    stringstream token;

    int counter = 0;

    for( int i = 0; i < (int)vectorOfLines.size(); i++ )
    {
        token << vectorOfLines.at(i);
        token >> trash >> trash >> trash >> data >> trash;
        vectorOfData.push_back(data);                       //  wrong method here?
        counter++;                                          //  counter to test if for iterates expected times
    }

    cout << "Counter: " << counter << endl;

    for( int i = 0; i < (int)vectorOfData.size(); i++ )
    {
        cout << "Variable number " << i + 1 << " : " << vectorOfData.at(i) << endl;
    }

    return 0;
}

Excuse my newbie question but after trying different approaches for last 5 days, I got to the point of swearing and getting discourages to continue learning.
Yes I’m very new to C++.
I’ve successfully done the same program in PHP (being total newbie in that too) and it seems like C++ is much, much harder to do.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T18:45:00+00:00Added an answer on June 11, 2026 at 6:45 pm

    You want to reset you string stream after reading an individual. From the looks of it, the string stream you are using goes into a fail state. At this point it won’t except any further input until the state gets clear(). Also, you should always verify that you read was successful. That is, I would start the body of your loop something like this:

    token.clear();
    token.str(vectorOfLines[i]);
    if (token >> trash >> trash >> trash >> data >> trash) {
        process(data);
    }
    else {
        std::cerr << "failed to read '" << vectorOfLines[i] << "\n";
    }
    

    I would also just use an std::istringstream.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a program that sends basic text files from one
I'm trying to write a secure transfer file program using Python and AES and
I am trying to write a program to collect security information about a file
I'm trying to write a program that would convert celcius to fahrenheit and visa-versa.
I am trying to write a program that will prompt you to enter someone's
I'm trying to write a program in Control Language which creates and populates a
I am trying to write a program that allows a binary to be run,
I'm trying to write a program in Python and I'm told to run an
I am trying to write a program that displays the integers between 1 and
I'm trying to write a program with Hibernate. My domain is now complete and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.