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

  • Home
  • SEARCH
  • 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 8842107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:52:58+00:00 2026-06-14T10:52:58+00:00

I am trying in vain to find a way to parse a text file

  • 0

I am trying in vain to find a way to parse a text file stored in a string object. The format of the string is as follows:

...
1  45
1  46
1  47
2  43
2  44
2  45
...

I am trying to iterate over the whole string, grab each line, and then split the string by the first integer and the second integer for further processing. However, doing something like this doesn’t work:

string  fileContents;

string::iterator index;

for(index = fileContents.begin(); index != fileContents.end(); ++index)
{
   cout << (*index);       // this works as expected

   // grab a substring representing one line of the file
   string temp = (*index); // error: trying to assign const char to const char*
}

I am trying to find a way to do this, but so far I haven’t had any luck.

  • 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-14T10:53:00+00:00Added an answer on June 14, 2026 at 10:53 am

    Use istringstreams and std::getline() to obtain the integers from each line:

    #include <iostream>
    #include <sstream>
    #include <string>
    
    int main()
    {
        std::istringstream in("1 45\n1 47\n");
        std::string line;
        while (std::getline(in, line))
        {
            std::istringstream nums(line);
            int i1, i2;
            if (nums >> i1 && nums >> i2)
            {
                std::cout << i1 << ", " << i2 << "\n";
            }
        }
        return 0;
    }
    

    See demo at http://ideone.com/mFmynj .

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

Sidebar

Related Questions

I've been trying to find examples everywhere but it's been in vain. I am
I have been trying, in vain, to make a program that reads text out
I have been trying in vain to try to find how to add listeners
Ok. A big newbee question here but I've been trying in vain to find
I am trying to check if a string is numeric but in vain. here
I simply trying (in vain) to remove all instances of the word 'at' during
I've been trying in vain to set the height on a container. I've been
I am trying, in vain it seems, to be able to pass additional parameters
Trying to make this jQuery filter that uses .find case-insensitive. For example, when the
Trying to get a wildcard search to pick up on any text in org_name

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.