Search code examples
c++csvsegmentation-faultcoredump

segmentation fault: core dumped while accesing csv records from a file


I am working on a college c++ project and creating a function that parses a csv file and reads records stored in it. But it gives me an error everytime I use that function

csv file example:

9138119,913811921297,Shivam,J,Jha,3811921297,[email protected],DEL,sji2ns,100,sj@bank

I tried using reserve() function of vector

void person::login(){
   bool found = false;

   fstream fin;
   fin.open("acc_info.csv", ios:: in);

   string CRN, Password;
   cout << "\nEnter CRN: ";
   getline(cin, CRN);

   cout << "\nEnter password :";
   getline(cin, Password);

   vector<string> record;
   string line, word, temp;

   if(fin){
      while(fin >> temp){
         record.clear();
         getline(fin, line);
         stringstream s(line);

         while(getline(s, word, ',')){
            record.push_back(word);
         }

         if(CRN == record[0] && Password == record[10]){
            found = true;
            cout << "\nLogin successfull!\n\n";
            break;
         }
      }
      if(!found){
         cout << "\nWrong CRN and password entered\n\n";
      }
   }
   else{
      cout << "\nTechnical fault occured.. try again after some time!!\n\n";
   }
}

It should match the CRN and password as entered by user and provide whether successful login or not

Here is the person class:

class person {

    public:
        void welcome();
        void show_account();
        void after_user_choice(int);
        void create_acc();
        void login();
        void accounts();
        void deposit();
        void cards();
        void loans();
        void insurance();
        void investments();
        int check(int, int, int);
        bool d;
        string retacno() const{
            return acc_no;
        }

        ~person();


        int x=5;

    protected:
        void show_acc_info(const unsigned long int);


        string firstname , middlename , lastname , address  , pan_no , fname ;
        string password, mobile_no ;
        string acc_no, crn, balance= "100";
        string email_id;
        string line, word, temp;

    private:
        string usr ; 
        string pswd;


};

Here's how I took input from user to write to CSV file:

void person::create_acc(){

     ofstream xl("acc_info.csv" , ios::app );
         cout<<"ENTER FIRST NAME\n";
     getline(cin,firstname);

     cout<<"ENTER MIDDLE NAME \n";
     getline(cin,middlename);

     cout<<"ENTER LAST NAME: "<<endl;
     getline(cin,lastname);  

     cout<<"ENTER FATHER'S NAME: "<<endl;
     getline(cin,fname);  

     cout<<"ENTER YOUR EMAIL_ID: ";
     getline(cin,email_id);

     cout<<"ENTER PAN CARD NUMBER: "<<endl;
     getline(cin,pan_no);

     cout<<"ENTER YOUR RESIDENTIAL ADDRESS: "<<endl;
     getline(cin,address);  

     cout<<"ENTER YOUR MOBILE NUMBER: "<<endl;
     getline(cin, mobile_no);

    acc_no = to_string(910000000000 + stol(mobile_no, nullptr, 10)); //generating acc no;

    crn = to_string(int(stol(acc_no)/100000)); //generating crn;
     string repswrd;
     bool z;

    do{

     cout<<"ENTER PASSWORD FOR YOUR CRN(USER_ACC): ";
     getline(cin,password);    

     cout<<"RE-ENTER YOUR PASSWORD: ";
     getline(cin,repswrd);

     if(!(password.compare(repswrd))){

           cout<<"Passwords matched succesfully!"<<endl<<endl;
           z = true;
           }
     else{

          cout<<"Passwords didn't match!!! Try again\n"<<endl;
          z = false;
               }

         }while(!z);

      cout<<"\n\nAccount created successfully\n\n";

      cout << "Customer Satisfaction is our main"
           << "priority, so we have deposited a "
           << "sum of Rs. 100 into your bank a/c"
           << endl << endl;

     xl<<crn<<","
      << acc_no<< ","
     << firstname << "," 
     << middlename << "," 
     << lastname<< ","
     << mobile_no<< ","
     <<email_id<<","
     << address<< ","
     <<pan_no<<","
     <<balance << ","
     << password<< "\n";

     xl.close();

     cout<<endl;  

     }

Solution

  • Your while(fin >> temp) reads the line and puts it in temp but you don't use it, so line will be empty, even if followed by another valid line in the file. >> leaves the newline character in the stream which is the only thing the following std::getline will read. It'll always read empty lines as long as the lines in the file don't contain characters that would make >> split it, like whitespace characters.

    Consider just using std::getline to skip formatted input and actually check that you got a valid record before indexing:

        if(fin) {
            while(std::getline(fin, line)) {
                record.clear();
                stringstream s(line);
    
                while(std::getline(s, word, ',')) {
                    record.push_back(word);
                }
    
                // check that you've got a valid record
                if(record.size() != 11)
                    throw std::runtime_error("invalid record size (" +
                                             std::to_string(record.size()) + ")");
    
                if(CRN == record[0] && Password == record[10]) {
                    found = true;
                    cout << "\nLogin successful!\n\n";
                    break;
                }
            }
            if(!found) {
                cout << "\nWrong CRN and password entered\n\n";
            }
        }