Loop to read through .txt file??

Hey guys I'm trying to read the data in a text file using a loop. The text file is has 2 unique columns - a bank account id and the balance for each account. How would I read the data for each account from the file, then create an object with the Class that I defined to use that data. I want to do store the object in an array.

Thanks!
I will advise you go to the Beginners forum, and please make an effort to at least put some codes together before you can get any help.

Regards
closed account (zb0S216C)
Such as request would involve structures. You would use the structure to represent a single account. The structure would have two data members:

- Account number
- Balance

The types of these data members is a design decision that should be based on the type of data within the file.

The second part would be to open a file stream that reads from the file. More specifically, std::ifstream. Unless you know the amount of account entries within the file at compile time, I'd recommend the std::vector as it allows run-time growth.

The final part would be to load the entries from the file. Using the std::ifstream object and your vector, extract an entry from the file and place the data within an instantiation of the structure. Then, push-back the vector with the newly extracted data. Do this until EOF (End-Of-File) is reached. if you know the amount of entries within the file, you can avoid the std::vector and use a C-style array.

Wazzak
Last edited on
Topic archived. No new replies allowed.