How to match a string from reading from a datafile, line by line?

Ok, I have the reading from the datafile line by line sorted, however I have a string that I want to search for in the datafile. This is what I had, however it doesnt work.

ifstream Appointments;
string line;
string date(charDate); .
size_t found;

found = line.find(date);

Appointments.open(DATAFILE);
if (Appointments.is_open())
{
// Read the file until it's finished, displaying lines as we go.
while (!Appointments.eof())
{
getline(Appointments, line); // getline reads a line at a time
if (found == string::npos)
{
cout << "You have the following appointment(s) today:" << endl;
cout << line << endl;
}
Appointments.close();
}
}
formated it for you,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
ifstream Appointments;
string line;
string date(charDate); .
size_t found;

found = line.find(date);

Appointments.open(DATAFILE);

if (Appointments.is_open())
     {
     // Read the file until it's finished, displaying lines as we go.
          while (!Appointments.eof())
          {
          getline(Appointments, line); // getline reads a line at a time
                if (found == string::npos)
                {
                cout << "You have the following appointment(s) today:" << endl;
                cout << line << endl;
                }
          Appointments.close();
          }
      } 



so...ummm, what are you comparing here?
if (found == string::npos)

edit, do you have the function and associated functions pasted here too?
Last edited on
For this: found = line.find(date); , if found == string::npos, that means date was not found. Don't think that's what you wanted.
yea you're right I have changed it to if (found != string::npos)
The function is a time function to find the local time,

1
2
3
4
5
6
7
8
     time_t rawtime;
     struct tm * timeinfo;
     char charDate [30]; //number of characters is 30

     time ( &rawtime );
     timeinfo = localtime ( &rawtime );

     strftime (charDate,30,"%d %B %Y",timeinfo); 
that is all I have, however it doesn't seem to work..
Topic archived. No new replies allowed.