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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:33:13+00:00 2026-05-30T08:33:13+00:00

I have this code…. file_parser::file_parser(string file){ txtfile.open(file.c_str(), ios::in); if (!txtfile.good()){ string error=err(0, +file+ not

  • 0

I have this code….

    file_parser::file_parser(string file){
        txtfile.open(file.c_str(), ios::in);
        if (!txtfile.good()){
            string error=err(0," "+file+" not found. Exit code ",1);
            throw file_parse_exception(error);
        }
        while (!txtfile.eof()){
            char str[200];
            txtfile.getline(str, 200);
            string str2=str;
            vfile.push_back(str2);
        }
        txtfile.close();
    }

and the problem is that if I have a line in the input file greater than 200 characters it hangs then crashes. I checked out the value of str at crash and it is preceded by a null char then it tries to push back a null(non-initialized) string onto the vector which causes the hang/crash. does anyone know a way to get around this? I thought by using getline it would truncate the char array at 199(+null) characters but apparently this isn’t happening. I’m stumped. The thing is that I want each pushback to have a max of 200 characters. I really don’t want the WHOLE line which is what ‘string str’ would do. and if a line is over 200 characters it should read the first 200 and then move on to the next line.

  • 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-30T08:33:14+00:00Added an answer on May 30, 2026 at 8:33 am

    Replace your input loop with this:

        std::string str;
        while (std::getline(txtfile, str)){
            vfile.push_back(str);
        }
    

    Using ios::eof() as a loop condition almost always creates a buggy program, as it did here. In this case, using eof() has two problems. First, eof() is only set after the read fails, not before, but you are checking it before the read. Second, eof() doesn’t check the range of other errors. When an input line has more than 200 characters, istream::getline sets failbit, but not eofbit.


    EDIT: With the added requirement of limiting the input lines to 200 characters, this should work:

    // untested
    std::string str;
    while(std::getline(txtfile, str)) {
        if(str.size() > 200)
            str.erase(200, std::string::npos);
        vfile.push_back(str);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code below where I loop through string and compare everything char
I have this code in a single file : public class genIntro { public
I have this code to create a config.ini file with Zend Framework but I
i have this code snippet private List<string> FolderOne(string Folder) { string filena; DirectoryInfo dir
I have this code which calculates the sum of "letter numbers" in a string,
have this code in a htaccess file to hide the final php extension of
I have this code in my routes.db file: resources :users do member do get
I have this code that throws a math domain error exception: v = -1.0
I have this code, the idea is to read from a file and the
i have this code that will check the array contains the specific string 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.