我试图从一个文件逐字读取并将其存储在cpp中相应的数组中。我在执行它时遇到问题。你们能帮我找到问题吗?这是我的代码从cpp文件中逐字读取
void Read_file()
{
int i=1;
ifstream in_file("cust_details1.txt");
if(in_file.is_open())
{
cout<<"List of all bills:\n"<<endl;
while(in_file){
cout<<"here";
in_file >> ac_no[i];
in_file >> ac_name[i];
in_file >> ac_amount[i];
in_file >> ac_emi[i];
in_file >> ac_sanc[i];
cout<<"Accout no = "<<ac_no[i]<<" Name = "<<ac_name[i]<<" Amount = "<<ac_amount[i]<<" Emi = "<<ac_emi[i]<<" Sanction = "<<ac_sanc[i] <<endl;
i++;
cout<<"----------------------------------------------------"<<endl;
}
in_file.close();
tot=i;
}
cout<<"Exiting"<<endl;
}
这段代码在第一次循环时执行得很好。即第一组记录被存储在阵列中。当循环进行第二次时,它会遇到seg故障。 here
未被第二次打印。所有的声明都是正确的。
Declarations:
string ac_name[30];
int ac_no[30];
string ac_sanc[2]; // it will hold only y or n
float ac_emi[30];
int ac_amount[30];
ac_no,ac_name等在哪里被初始化,它被初始化为什么? – arunkumar
是否是条件检查操作?我不知道ifstream当他们到达EOF时自己调零...? (可能会更好/更清晰地使用'while(in_file.good())') – bcr
对不起guyz,prob被解决了。这是一个愚蠢的错误,正如'Anteru'所指出的那样。 – CHID