How do I read an interger file up to a specific character?

Hello, I just joined the C++ community to get help and help others if I can.
I currently have an assignment where I need to read 4 things from a file.
1.Bank account as a string
2.name as string
3.check balance as float
4.savings balance as float

We are using structures where all variables are stored for each account.
here's my fucntion where I read from the file

int loadCostumers(account accounts[])
{
int numCostumers = 0;
ifstream infileAccounts;
int count = 0;

//open file
infileAccounts.open("customer.dat");

//check if file opened
if (!infileAccounts)
{
cout << "File containing accounts not found" << endl;
cout << "Please close program and make sure file exists" << endl;
}
else
{
getline(infileAccounts, accounts[count].accountNumber, '#');
getline(infileAccounts, accounts[count].name, '#');


//read from file

}
return numCostumers;
}



the getline function works for the first two strings but I don't know how to read the next 2 numbers as they are not strings, I've tried the ignore() function but it doesnt work either. I need to know how I can read an integer or a float up to a certain character in this case a # sign.
here's how the text file looks:

230#Louis Smith#1000#500
130#Paul Lee#20000#50000


if anyone can help me I would much appreciate it.
you could read in a string again...then convert it to a float...

stof()

http://www.cplusplus.com/reference/string/stof/
Last edited on

read the float as string and convert to float using atof function
and for last number use this getline(infileAccounts, accounts[count].savings, '\n');
Last edited on
yes stof
Topic archived. No new replies allowed.