Reading from a text file

Hello, I'm a computer engineering major. My CS 1 class was last year and I'm now in the second level of this course, programming in C++. Since it was a year ago I'm having some issues reading from a text file. Here is what I know:

I need to #include <fstream>

declare an input such as fstream inFile;

open a filename inFile.open("file");

But I can't remember how to get a string or character.

What I'm needing to do is pull from a file in the following format:

John Doe
Jane Doe
Jump High

I'm needing to read the first names into one parallel array, and the last names into another parallel array. So I can output them in the same order but like:

Doe, John
Doe, Jane
High, Jump

I've got the output and all that, I just wanted to show the whole project. I'm thinking I need to read to a whitespace, store, then read to another whitespace, store, add to a counter to drop both arrays down to the next storage value, and also drop to the next line in the text file.

Any help would be greatly appreciated!!! Thanks!
I personally would read the file line by line storing each line into a string object (#include <string> ) then create a function to seperate the one string into two, and place each string accordingly into the first and last names arrays/holders. I actually wrote a program to do this for someone earlier, http://www.cplusplus.com/forum/beginner/18509/#msg95324

Does this help?

Yes, it does, but I think my instructor is wanting us to do it a certain way. Say I declare inFile as my fstream, and open the filename. I could make a while loop:

while (inFile) or while (i < SIZE) and either fill the arrays until there are no more slots to fill, SIZE variable being the array size, and "i" being a counter.

What are the commands to get a string and get a line? After the file is open isn't it jus "cin >>..." Also once I get to the end of a line, I remember vaguely something needing to be in place to make it jump to the next line. This may be why you were suggesting getting the whole line first because it would be hard to have a loop running and only jump down once it gets to the end. I'm drawing a blank here, I should have been practicing this stuff a lot more over that year!
Topic archived. No new replies allowed.