简体   繁体   中英

Read word by word from file in cpp

I tried to read word by word from a file and store it in corresponding array in cpp. I am facing problems in executing it. Can you guys help me find the problem. This is my code


void Read_file()
{
  int i=1;

  ifstream in_file("cust_details1.txt"); 
  if(in_file.is_open())
  {
    cout<<"List of all bills:\n"<&ltendl;
    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 = "<&ltac_no[i]<<" Name = "<&ltac_name[i]<<" Amount = "<&ltac_amount[i]<<" Emi = "<&ltac_emi[i]<<" Sanction = "<&ltac_sanc[i] <&ltendl;
      i++;
      cout<<"----------------------------------------------------"<&ltendl;
    }
    in_file.close();
    tot=i;
  }
  cout<<"Exiting"<&ltendl;
}

This code executes well for the first time of the loop. ie first set of records is being stored in the array. When the loop goes for second time, it runs into a seg fault. the here is not being printed for second time. All declarations are made correctly.

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];

Your while loop should look like this:

while(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++;
}

Such loop ensures that if there is any error in file, it will stop reading.

By the way, the size of each array should be large enough to hold all values in the file. You've declared each array of size 30 except string ac_sanc[2] . Why the size of ac_sanc is only 2 ? You know what it means? It means ac_sanc[i] would invoke undefined behavior for i >=2 . Your program might (and most likely will) crash. Also, if it can be only y or n , then why don't you declare it as char array?

Since you're using C++, I would suggest you to define a struct and use std::vector as follows:

#include <string>
#include <vector>

struct Account
{
    std::string ac_name;
    int         ac_no;
    char        ac_sanc;
    float       ac_emi;
    int         ac_amount;
};

std::vector<Account> accounts;
Account ac;

while(in_file >> ac.ac_no &&
      in_file >> ac.ac_name &&
      in_file >> ac.ac_amount &&
      in_file >> ac.ac_emi &&
      in_file >> ac.ac_sanc)

     accounts.push_back(ac);
     //...
     //you may use 'ac' to print each values
}

You're writing into the second element in the first loop ( ac_no[1] , instead of ac_no[0] .) If your array is of size 2, then this might explain why it crashes.

If you can't control the input, you shoud use std::vector and push_back or at least check if i < arraySize to catch mistakes like this.

There are a few problems with your code.

while(in_file)

will not fail when you reach the end or have an error, instead prefer:

while(in_file.good())

Next:

string ac_sanc[2];  // it will hold oly y or n

This should probably be:

char ac_sanc[30];

However, as suggested elsewhere , you should probably read up on serialisation techniques. What is going to happen when your "cust_details1.txt" has thirty (you're only using indexes from 1, where arrays are zero-based) entries?

And please, please learn to indent your code!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM