Extracting information from a text file

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.
http://www.cplusplus.com/doc/tutorial/files/

I also recommend you use a map<> instead of an array of structs.
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.
I am a student at UAT hopefully this will help.

The ifstream should process each part individually, and it should self separate at any white space.

for instance the code would look a little like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//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 


Dan Schwartz
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.

Dan Schwartz
Last edited on
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?
closed account (zb0S216C)
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.
Last edited on
I'm not sure what you mean. Does the ';' go in the file or in my code?
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.)

Dan Schwartz
the ; goes in your txt file like so:

;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...


Dan Schwartz
Last edited on
Topic archived. No new replies allowed.