C++ reading in from text file for comparisons

Hi, so I'm very confused on how reading in from a text file works. If I wanted to read in from a text file that contained information delimited by colons and ends with a newline:

ex.
Dog:10:20:Terrier:y:y:Cute\n
Turtle:10:20:Galapagos:y:y:Slow\n

and wanted to compare read-in animal's breed with a breed the user is looking for, how would I do that?

Here is the code I thought of so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
bool animal_shelter::if_found(int num) //num is the number of animals stored
{
  int g = 0;
  int i = 0;
  ifstream file_in;
  char breed_match[SIZE]; 

  animals = new animal[num]; //animals is a pointer to a struct animal in my class

  cout << "What breed are you looking for? ";
  cin.get(breed_match,SIZE,'\n'):
  cin.ignore(100,'\n');

  file_in.open("animals.txt")

  if(file_in)
  {
    file_in.get(animals[i].animal_name,SIZE,':');
    file_in.ignore(100,':');
 
    while(file_in && !file_in.eof() && i < num);
    {
      file_in >> animals[i].age;
      file_in.ignore(100,':');
      file_in >> animals[i].weight;
      .
      . //continue reading in rest of information... 
      .
      ++i;

      file_in.get(animals[i].animal_name,SIZE,':')
      file_in.ignore(100,':');
    }
    file_in.clear();
    file_in.close();
  }
  for(g = 0; g < num; ++g)
  {
    if(strcmp(breed_match, animals[i].animal_breed) == 0)
      return true;
  }
  return false;
}


this currently results in a segmentation fault...

Last edited on
I am working on a similar program right now and I get the same error message. My problem is a "while" loop not ending correctly. It tries to read out more from the file, despite being at the end of it. So I think your conditions to terminate the while loop are as wrong as mine :)

I am still working on a solution sadly...

Edit:

I changed some stuff and got the following message:
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::copy
Aborted

You might try to do something with an exception catcher.
Last edited on
Have a look at this thread. Maybe you can adapt my example there.
http://www.cplusplus.com/forum/beginner/185534/
Hey, thank you guys for your inputs. I've been stuck on this problem for a week now so any help is welcome :)

futanari, have you made sure to prime the pump first before using eof() (i.e. attempt to read in the first item)? Attempting to do so will set the eof flag which then lets the program know when the end of file is by looking for the set flag. The .eof() function doesn't exactly work the way we think it should lol.

Thomas, thanks for the page reference. Unfortunately, we are not allowed to use the string library nor have we had experience yet with the <sstream> library.

Oh and just to clarify what my question really was, I was curious as to how the ifstream worked when I need to compare arrays of characters. For example, I want to read in an animal's breed from the file and then compare that to the breed the user is searching for.
Question is, do I have to create local variables to store the read in information?

For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
char temp_name[SIZE] //SIZE is a constant int of 40
char breed_match[SIZE] //asks user what breed they're looking for

//Reading in the animal's name first
file_in.get(temp_name,SIZE,':'); //colon delimiter because that's what I used as a delimiter
file_in.ignore(100,':');

//From this point, I'm assuming that the word "Dog" is now stored in my 
//char temp after it was read in from my text file. I then proceed 
//by creating other local variables to store the rest of the animal's information.

//After all of this is said and done, I now want to compare breed_match with temp_breed
if(strcmp(breed_match, temp_breed) == 0)
  return true;
return false;


So this is just an example. Is this correct thus far? Or does it not work the way I think it does? Thanks!
Last edited on
Topic archived. No new replies allowed.