我試圖從一個文件逐字讀取並將其存儲在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