分段错误:从文件访问 csv 记录时发生核心转储
segmentation fault: core dumped while accesing csv records from a file
我正在研究一个大学 C++ 项目并创建一个函数来解析 csv 文件并读取存储在其中的记录。但是每次我使用那个函数时它都会给我一个错误
csv 文件示例:
9138119,913811921297,Shivam,J,Jha,3811921297,sj@g.c,DEL,sji2ns,100,sj@bank
我尝试使用 vector
的 reserve() 函数
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";
}
}
与用户输入的CRN和密码相匹配,并提供是否成功登录
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;
}
您的 while(fin >> temp)
读取该行并将其放入 temp
但您不使用它,因此 line
将为空,即使文件。
>>
将换行符留在流中,这是后续 std::getline
将读取的唯一内容。只要文件中的行不包含会使 >>
拆分它的字符,如空白字符,它就会始终读取空行。
考虑只使用 std::getline
跳过格式化输入并在索引之前实际检查您是否获得了有效记录:
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";
}
}
我正在研究一个大学 C++ 项目并创建一个函数来解析 csv 文件并读取存储在其中的记录。但是每次我使用那个函数时它都会给我一个错误
csv 文件示例:
9138119,913811921297,Shivam,J,Jha,3811921297,sj@g.c,DEL,sji2ns,100,sj@bank
我尝试使用 vector
的 reserve() 函数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";
}
}
与用户输入的CRN和密码相匹配,并提供是否成功登录
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;
}
您的 while(fin >> temp)
读取该行并将其放入 temp
但您不使用它,因此 line
将为空,即使文件。
>>
将换行符留在流中,这是后续 std::getline
将读取的唯一内容。只要文件中的行不包含会使 >>
拆分它的字符,如空白字符,它就会始终读取空行。
考虑只使用 std::getline
跳过格式化输入并在索引之前实际检查您是否获得了有效记录:
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";
}
}