I am doing a project and I have to read from a file and input the values into an array. I know how to do this. But now we have to read from a file in put it in multiple arrays. The file that I'm reading looks like this:
Simply write three input >> statements reading the different data fields into different arrays, all of which are indexed by [i].
BTW, the first line of your input file contains a record count. Either read it first and use it for the terminal condition of your loop, or read and discard it, and then read the records until you reach EOF. If you do neither of those, your program most likely gets screwed up.
HTH
Last edited by Eri523; November 4th, 2010 at 10:49 PM.
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
But how would I read the statements that are further in the file?
I know for the words I can just put char name[i] and put those into an array but how do I get the two numbers on the same line into a different array
Oops! I failed to notice that you are using eof() the wrong way in your original post. If you opened the file successfully, eof() will return the boolean value false, which is converted to integer 0. Therefore, the terminal condition of the loop will be true before the very first iteration and as a consequence you actually read nothing.
Using eof() that way isn't a good idea anyway: It will only become true after you actually attempt to read past the end of file, and thus you will read exactly one bogus record at the end. So you are better off to use the record count in the first line of the file as a criterion, which already was one of the options I suggested in post #2.
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
Bookmarks