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 6699149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:39:41+00:00 2026-05-26T06:39:41+00:00

I have the following code to read a text file. const string FILENAME =

  • 0

I have the following code to read a text file.

const string FILENAME = PACKAGES_DIR + pname;
  //the arguments to ifstream is a cstring and hence the conversion must be made
  ifstream freader;
  freader.open(FILENAME.c_str(),ios::in);
  if(freader.is_open())
  {
    while(freader.good())
    {
      string line;
      getline(freader,line);
      cout<<line<<endl;
      if(line.find("PackageId:"))
      {
        cout<<line.substr(11)<<endl;
      }
      else if(line.find("Name:"))
      {
        cout<<line.substr(5)<<endl;
      }
      else if(line.find("Version:"))
      {
        cout<<line.find(8)<<endl;
      }
      else
      {
        cout<<line<<endl;
      }

    }
  }

The contents of the text file in question is

PackageId:994
Name:basket
Version:1.80-1
Deps:kdebase-runtime,libc0.1,libc0.1-udeb,libc6,libc6-udeb,libc6.1,libc6.1-udeb,libgcc1,libgpg-error0,libgpgme11,libkdecore5,libkdeui5,libkfile4,libkio5,libkparts4,libkutils4,libphonon4,libqimageblitz4,libqt4-dbus,libqt4-network,libqt4-qt3support,libqt4-svg,libqt4-xml,libqtcore4,libqtgui4,libstdc++6,libunwind7,libx11-6,phonon

The output that I get is

PackageId:994
geId:994
Name:basket

Version:1.80-1
0-1
Deps:kdebase-runtime,libc0.1,libc0.1-udeb,libc6,libc6-udeb,libc6.1,libc6.1-udeb,libgcc1,libgpg-error0,libgpgme11,libkdecore5,libkdeui5,libkfile4,libkio5,libkparts4,libkutils4,libphonon4,libqimageblitz4,libqt4-dbus,libqt4-network,libqt4-qt3support,libqt4-svg,libqt4-xml,libqtcore4,libqtgui4,libstdc++6,libunwind7,libx11-6,phonon
e-runtime,libc0.1,libc0.1-udeb,libc6,libc6-udeb,libc6.1,libc6.1-udeb,libgcc1,libgpg-error0,libgpgme11,libkdecore5,libkdeui5,libkfile4,libkio5,libkparts4,libkutils4,libphonon4,libqimageblitz4,libqt4-dbus,libqt4-network,libqt4-qt3support,libqt4-svg,libqt4-xml,libqtcore4,libqtgui4,libstdc++6,libunwind7,libx11-6,phonon

terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::substr

The output that I wanted is:

PackageId:994
994
Name:basket
basket
Version:1.80-1
1.80-1
...

What have I done wrong?

  • 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-05-26T06:39:42+00:00Added an answer on May 26, 2026 at 6:39 am

    Problem 1

    while .good or while !.eof is almost always wrong. Throw away whatever book told you to do that, and do this instead.

    In this case, the changed code looks a bit like this:

    const string FILENAME = PACKAGES_DIR + pname;
    //the arguments to ifstream is a cstring and hence the conversion must be made
    ifstream freader(FILENAME.c_str(), ios::in);
    if (freader) {
       string line;
       while (getline(freader,line)) {  // <-----
          cout << line << endl;
    
          if (line.find("PackageId:"))
             cout << line.substr(11) << endl;
          else if (line.find("Name:"))
             cout << line.substr(5) << endl;
          else if (line.find("Version:"))
             cout << line.find(8) << endl;
          else
             cout << line << endl;
       }
    }
    

    Problem 2

    You’re not using std::string::find correctly.

    line.find("PackageId:") returns either “the position of the first occurrence in the string of the searched content”, or the member value npos if the match is not found.

    This combined with not performing bounds checks on the first parameter to std::string::substr is causing issues with your strings.

    Instead, write:

    if (line.find("PackageId:") != std::string::npos)
    

    Problem 3

    cout<<line.find(8)<<endl; should say substr, not find.


    Your code with some of the above fixed:

    const string FILENAME = PACKAGES_DIR + pname;
    //the arguments to ifstream is a cstring and hence the conversion must be made
    ifstream freader(FILENAME.c_str(), ios::in);
    if (freader) {
       string line;
       while (getline(freader,line)) {  // <-----
          cout << line << endl;
    
          if (line.find("PackageId:")    != std::string::npos && line.size() > 11)
             cout << line.substr(11) << endl;
          else if (line.find("Name:")    != std::string::npos && line.size() > 5)
             cout << line.substr(5) << endl;
          else if (line.find("Version:") != std::string::npos && line.size() > 8)
             cout << line.substr(8) << endl;
          else
             cout << line << endl;
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have the following code where I should read a Text File (This
When I using the following code to read file: lines=file(data.txt).read().split(\n) I have the following
I have got the following code from here to read an Excel file using
I've encountered a problem. I have a code which should read the following text
I am running the following code to try and read from a text file.
I have the following code snippet to read the full contents of a text
I have the following simple code, that reads contents of a text file into
I have the following code to read contents from a file to open multiple
I have the following code: f = open(path, 'r') html = f.read() # no
Have a look at the following code you are not required to read the

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.