Reading in file with mixed data types to struct

closed account (NCRLwA7f)
Hello and thank you for taking the time to help.
I am having an issue oh how to read an input file with mixed data types to read into a struct. The file is a store report of balances customers owe. I have looked on google and youtube and I'm not finding exactly what I'm looking for. most example either read in data that is in one column format or they read in the data from cin.
My assignment has 10 customers and the data looks similar to this (account#, name, address, balance)

123456. Smith, Joe R. 234 Happy ln. -30.92

ok so some of the things that I have tried that looked promising did not help.

1
2
3
4
5
6
7
8
9
10
11
12
  
    char acc[10];
    char name[20];
    char adrs[20];
    float bal;

    inFile.get(acc, 10);
    inFile.get(name, 20);
    inFile.get(adrs, 20);
    inFile >> bal;
//this only returns the 1st line and the second line nothing




1
2
3
4
5
6
7
8
9
    string acc;
    string name;
    string adrs;
    string bal;
   
   getline(inFile, acc);
//Same for the others but no luck here either



I am aware of using to <sstream> to convert between data types but I cannot get all the data in a consistent format. Some names have middle initials and others don't so that messes up some loops if I try to read everything in as a string.

We are still not at pointers or vectors. we just started structs last week, and I have been trying different things for two days.

I feel that I need to read the data in as char but I don't think I'm doing it correctly, on my assignment my professor mentions where some values end and begin (spaces and length ) so that the hint I'm going off of.

Thank you.
> 123456. Smith, Joe R. 234 Happy ln. -30.92

Since the data appears to have a period as the delimter between fields in a line, we can exploit that
(if the name and address fields themselves do not contain embedded periods).

Something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
std::ifstream file( "accounts.txt" ) ;
std::string line ;

while( std::getline( file, line ) ) // for each line read from the file
{
    std::istringstream stm(line) ;
    std::string acct, name, address ;
    double balance ;
    constexpr char PERIOD = '.' ;

    // read everything up to the first period into acct
    std::getline( stm, acct, PERIOD ) ;

    // read everything up to the next period into name
    std::getline( stm, name, PERIOD ) ;

    // read everything up to the next period into address
    std::getline( stm, address, PERIOD ) ;

    // read what is left into balance
    stm >> balance ;

    if(stm) // if the fields were successfully read
    {
        // do what ever with the data
    }
}
closed account (NCRLwA7f)
I just looked at my data and realized that I added a period after the account number to the data I provided. Non-the less, your example was very helpful, I was not aware of delimiters and I have looked them up and have found a source that really helped. I took your advice and read the data in as a while loop, the only different is that I used inFile.get() in combination with inFile.ignore()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

   int acc;
   char nm[16];
   char adrs[20];
   double bal;
   
   inFile >> acc;
   
   inFile.ignore(3);
   inFile.get(nm, 16);
   
   inFile.ignore(1);
   inFile.get(adrs, 20);
   
   inFile.ignore(11);
   inFile >> bal;
Last edited on
Topic archived. No new replies allowed.