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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:50:23+00:00 2026-05-27T09:50:23+00:00

I have following structure. #include <iostream> #include <fstream> #include <string> #include <sstream> #include <vector>

  • 0

I have following structure.

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>

struct station {
    std::string id;
    std::string code;
    std::string station_name;
    station(std::vector<std::string> &in) : id(in[0]), code(in[1]), 
                                            station_name(in[2]) {}
    station(): id (""), code (""), station_name(""){}

    bool operator<( const station& rhs ) const {
        return this->station_name < rhs.station_name;   
    }
};
int main(int argc, char **argv) {    
    std::ifstream ifs(argv[1]);
    if ( ifs.peek() == EOF )  {
        exit ( 1 );
    }
    // Read the input file and update the database
    std::string line;
    station prev, current;
    std::set<station> my_set;
    while( ifs.good()&& std::getline(ifs,line) ) {
        std::stringstream  lineStream(line);
        std::string        token;
        std::vector<std::string> input;        
        while(std::getline(lineStream,token,',')) {
            input.push_back(token);
        }
        station st(input);
        my_set.insert(st);

    }
}

I am reading a file which has information related to railway stations in the following format
ID,Station Code,Station Name

I am reading this file line by line and creating a object of station and then pushing the same into the std::set<station>

It gets crashed after some time, around after reading 21448 line. I have around 403523 lines

What is the problem here.

This program works properly on Linux but not on windows

I get debug assertion failed

  • 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-27T09:50:24+00:00Added an answer on May 27, 2026 at 9:50 am

    The constructor that takes an array worries my.

    Edit

    Based on the updated question:

    The problem is the constructor that takes a vector.
    You are accessing the elements without check if they exist.

    So if any line of input is bad (ie not all values are present) then the vector will not be as big as required and result in undefined behavior.

    If you change these lines:

    while(std::getline(lineStream,token,',')) {
                input.push_back(token);
            }
    

    Into:

    std::getline(linestream, id,   ',');
    std::getline(linestream, code, ',');
    std::getline(linestream, name, ',');
    

    Then called the station constructor with these parameters (as would have been nice in OO code). Then you would not have seen the crash. Some error checking would also be nice.

    Note:

    while( ifs.good()&& std::getline(ifs,line) ) {
    

    There is no need to check good() here. If the stream is not in a good state then getline() will do nothing. The conversion of the stream (the return value of getline()) to a bool is also checking if the state is the stream is valid for further reading and if not will convert to false (eventually).

    // This is more idiomatic.
    while(std::getline(ifs,line) ) {
    

    Original

    try this:

    struct station
    {
        std::string id;
        std::string code;
        std::string station_name;
        friend std::istream& operator>>(std::istream& stream, station& data)
        {
            std::string  line;
            std::getline(stream, line);
    
            std::stringstream  linestream(line);
            std::getline(linestream, data.id, ',');
            std::getline(linestream, data.code, ',');
            std::getline(linestream, data.station_name);
    
            return stream;
        }
    };
    
    int main()
    {
        std::ifstream   file("station.txt");
        std::vector<station>  stations;
    
        std::copy(std::istream_iterator<station>(file),
                  std::istream_iterator<station>(),
                  std::back_inserter(stations)
                 );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: #include <iostream> using namespace std; struct stud{ int roll;
The problem I have is illustrated in the following code. #include <iostream> #define X
I have some problem compiling my code. I have the following structure: #include <cstdlib>
In my socket code I have the following structure: Server: #include <stdio.h> #include <stdlib.h>
suppose you have the following structure: #include <windows.h> // BOOL is here. #include <stdio.h>
I have following table structure: Table: Plant PlantID: Primary Key PlantName: String Table: Party
I currently have the following directory structure for my code: src |-- main |
I have the following directory structure. root --src ---tests src contains the source &
I would like to have a the following structure of folders: Myproj\src Myproj\include Myproj\lib
I want to have the following class structure: #include <tr1/memory> class Interface; class Impl;

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.