Let's say I have an array of structs called animals, and for every struct in the array I need to get struct.name and struct.number from a text file that looks like this:
1 2 3 4
PIGS 12
MONKEYS 4
ZEBRAS 124
COWS 2
How would I go about this? I know I'd have to set up a file stream and a for loop, but I don't know how I would get the name and the number individually.
I've read that page, but I still don't really understand how I would get the names and the numbers individually or how I would make sure every element of the array got the data from a different line.
And I just thought of the array of structs example because I thought it best illustrated the problem I want to understand. I'm sure there are better ways to go about this, but right now I'm just trying to understand how to get data from files.
//other includes here
#include <fstream>//needed for file stream
int main (){
ifstream inFile; //sets up the input file stream
//your structure goes here
inFile.open("your.txt"); //tells the computer to open your text file or whatever
//is in the spot between the quotes.
//you would insert your loop here based off how many entries you have hopefully it
//corresponds to the number of structures you have set aside for the task.
inFile >> struct.name;
inFile >> struct.number;
// this should get you headed in the right direction.
//based off the information you have provided
the file stream automatically seperates out white space; such as spaces, tabs, and newline. You only have to make sure the data being read in is put in an appropriate variable. In my above example, the two inFile statements would be encapsulated in a loop, that would switch to the next element of the structured array. That would be how you would get past the new line white space.
Thank you very much! I didn't realize the file stream automatically separated out white space.
Let's say that the text file has a title or some other useless information on the first line, though. I guess I could just create an extra variable to store it in, but that doesn't seem like a very elegant solution, so is there a way to skip lines or other parts of the file?
Let's say that the text file has a title or some other useless information on the first line
Could you add a character such as ';' at the start of a new line to tell your file reader( or file parser, which ever you fancy ) to a void that line? I do it all the time.
if it is a full line you could do that, it would be like a primer to a loop. you can get an entire line without parsing by using the getline() method.
So fo your first line you could use a getline() and then use the inFile command I suggested. Or you could make it super simple and get rid of the header stuff in the text all together. ( this would make finding the EOF easier, which would allow you to build a better loop.
I hope this helps. I do have a limited amount of experience ( which is why I am hanging out in the beginners section.)
;some header information here
PIG 12
COW 3
DONKEY 5
...
it parses out on most stuff like the // comments out code in C++ i just don't know for sure if it works with C++ ( I know it works with other languages, but I am not positive it does with C++)
try it out and let me know we can learn something new together...