Problem with reading from file

Hello! I have some troubles reading from a file!
The file I want to read looks something like this:

Name
ID
Account Number
Account Type
Account Balance
Account maxwithdrawals(if savings account) and credit (if transaction account)

This file can contain a minimum of one account and maximum of three.
Name and ID is read once and then I want to read what's below until file is done.

My code looks like this, but I will tell you it is not working at all:
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
  bool Customer::readFromFile(std::string pCustomerID) {

    std::string tmpANr;
    std::string name;
    std::string accountType;
    double tmpBalance, tmpCredit;
    int numberOfWithdrawals = 0;

    std::ifstream inFile("../../_Resources/Lab_3/" + pCustomerID + ".knt", std::ios::in);

    if (inFile.is_open())
    {
        getline(inFile, name);
        getline(inFile, pCustomerID);

        customerName = name;
        customerID = pCustomerID;

        while (getline(inFile, tmpANr))
        {
            getline(inFile, accountType);

            if (accountType == "Transaction account")
            {
                inFile >> tmpBalance;
                inFile >> tmpCredit;
                inFile.get();
                accountPointerContainer.push_back(std::unique_ptr<Account>(new transactionAccount(tmpANr, accountType, tmpBalance, tmpCredit)));
            } else if (accountType == "Savings account")
            {
                inFile >> tmpBalance;
                inFile >> numberOfWithdrawals;
                inFile.get();
                accountPointerContainer.push_back(std::unique_ptr<Account>(new savingsAccount(tmpANr, accountType, tmpBalance, numberOfWithdrawals)));
            } else
            {
                inFile >> tmpBalance;
                inFile >> numberOfWithdrawals;
                inFile.get();
                accountPointerContainer.push_back(std::unique_ptr<Account>(new longSavingsAccount(tmpANr, accountType, tmpBalance, numberOfWithdrawals)));
            }
        }

        inFile.close();
        return true;
    } else
    {
        return false;
    }
}


What have I done wrong?
Hello seatea,

First question is what does the input file actually look like? What you have posted tells me what you need to read, but not what it is actually reading.

Line 9 creates a file stream variable "inFile", but how do you know that the file is open. Just because you tell it to open a file does not mean that it is.

Lines 13 - 17 are a little confusing as to why you did that.

Without the proper input file and the rest of the code it is hard to say what is wrong with out being able to test it.

Andy
The layout of the file is what I wrote, the file looks like this:

Handy Andy
12345
12345-1
Transaction account
5000
200
12345-2
Savings account
250
2
12345-3
Long Savings account
0
0

Lines 13-17 are working, that is just data members being set by the first two lines read from the file, and the file does read. But when I use it like this I only get Transaction accounts when loading the file, and only the first account number (12345-1 in the example above) but all three accounts are created. The other two gets no account number at all. So probably I'm not reading the file correctly, but I don't know what's wrong.

What I want it to do is read "Handy Andy" and set it as name (which it does), then read 12345 and set it as customer id (which it does).
But then I want it to read the account type below and add balance and credit/number of withdrawals and create a unique_ptr from the correct derived class that gets pushed to a vector of the base class.
Last edited on
have some troubles reading from a file!

So it took you 50 lines of code to find out you got into trouble.
That is a bad situation to get into.

So what you need to do is put your 50 lines to one side and learn how to simply read a file and print out what you have read. Nothing more complicated than that!!

Start with this tutorial https://www.cplusplus.com/doc/tutorial/files/

And your mini-program should look 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
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  string Name;
  int ID;
  // blah, blah

  ifstream myfile ("example.txt");
  if (myfile.is_open())
  {
    while ( myfile >>  Name >> ID .... )
    {
      cout << Name << ' ' << ID ... << '\n';
    }
    myfile.close();
  }

  else cout << "Unable to open file"; 

  return 0;
}

I got it working! Thanks anyway!
Topic archived. No new replies allowed.