Hello everybody.
I'm just about to finish up my first semester of learning C++, and I'm trying to figure out some things that have been confusing to me.
So my textbook first gives an example of how to pass an fstream object to a function. The lesson simply states that when doing this, you need to pass by reference. I'm a little foggy on exactly how fstream objects work, but maybe I don't need to know that right now.
So the program passes the fstream object and a name to a function that tests to see if the file exists. If it does, then the fstream object (alone) is passed to another function from main called "showContents" which looks like this:
1 2 3 4 5 6 7 8 9
|
void showContents(fstream &file)
{
char line[MAX_LINE_SIZE] //which is 81
while (file >> line)
{
cout << line << endl;
}
}
|
I believe that this function is the source of my confusion. The problem I'm having is that the next lesson in the textbook explains how to read from a text file while including the proper spacing.
The file that the second lesson uses looks like this:
Jane Murphy
47 Jones Circle
Almond, NC 8639
And the output looks like this: Jane Murphy47JonesCircleAlmond,NC8639
The textbook explains how to use the getline member function on a filestream object.
So what has be scratching my head is that the first lesson's code didn't have this problem. The program was structured differently, but I can't figure out what the key difference is.
Sorry if I'm making this more confusing than it needs to be. I'm trying to avoid throwing the entire codes of both programs at you. If somebody could explain how that showContents function is working, I might be able to figure the rest out on my own.
Thank you very much.