Modifying txt file using classes

A text file contains data on some individuals, formatted as follows:

lastName,firstName,gender,studentNumber,birthDate,OHIP,email

It does not have spaces, only commas as separators and a new line for each individual.

I've imported this text file using ifstream() and have been able to assign one line's worth of data into a class. My question is, how can I get the user to search the entire text file (assuming it has 10-50 individuals' data stored) for one person's data, according to their last name?

I know this may sound very vague, so does anyone have any pointers? Like, a search() function maybe?

This is my code 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
    //Reading: allow user to search by last name (if possible, by any property of {Individual}) and display information that have been stored into the class
//Only 1 class reqd...
    //Writing: allow user to search ^ and modify any property
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;

class Individual
{
      public:
             Individual(){};
             void getData();
             void function2();
             string strFunct();
             string rawLine;
      private:
             
             string last;
              string first;
              string gender;
              string studentNumber;
              string dateOfBirth;
              string ohip;
              string email;


};
void Individual::getData(){
     string line;
     char *cstr, *p;
     short j=0;
     ifstream fileA("file.txt");//opens the file
     if(fileA.is_open()){
                         while(!fileA.eof()){//while the file has not reached its end...
                                             
                                             cstr=new char[line.size()+1];
                                             strcpy(cstr, line.c_str());
                                             p=strtok(cstr, ",");
                                             while(p!=NULL){
                                                            //cout<<p<<endl;
                                                            ++j;
                                                            switch(j){
                                                                      case 1:last=p;
                                                                      case 2:first=p;
                                                                      case 3:gender=p;
                                                                      case 4:studentNumber=p;
                                                                      case 5:dateOfBirth=p;
                                                                      case 6:ohip=p;
                                                                      case 7:email=p;
                                                            }
                                                            p=strtok(NULL,",");
                                             }
                                             getline(fileA,line);
                         }
                         rawLine=line;
                         fileA.close();
     }
     else cout<<"Unable to access file"<<endl;
     cout<<last<<endl<<first<<endl<<gender<<endl<<studentNumber<<endl<<dateOfBirth<<endl<<ohip<<endl<<email<<endl<<endl;
};

void Individual::function2(){
     char * cstr, *p;

  string str = rawLine;

  cstr = new char [str.size()+1];
  strcpy (cstr, str.c_str());

  // cstr now contains a c-string copy of str

  p=strtok (cstr,",");
  while (p!=NULL)
  {
    cout << p << endl;;
    p=strtok(NULL,",");
}
     
};

int main ()
{
    Individual IndividualA;
    IndividualA.getData();
IndividualA.function2();


  system("PAUSE");
  return 0;
}
The standard string library's getline() function is a nice and often overlooked solution to string parsing.

To parse a file by line, you say:
1
2
3
4
5
6
7
fstream my_file;
string  line_of_text;

while (getline( my_file, line_of_text ))
  {
  // do something with the line of text...
  }


Well, the very same principle can be applied to a string by using a stringstream (#include <sstream>):
1
2
3
4
5
6
7
string line_of_text = "Eat,Flaming,Death,silly,strings!";
stringstream ss( line_of_text );
string text_item;
while (getline( ss, text_item, ',' ))
  {
  // do something with the text item...
  }


This should be enough to get you started. Good luck.
Last edited on
Thank you !!!

I didn't know getline() can directly take a line at a specified line!
Well, it can't, but you can always turn a string into an iostream, or stringstream, that getline() works with just fine!

Neat, isn't it?

The only limitation is that it cannot break on sets of characters, so if you want to find everything up to the first comma or semicolon (for example), getline() isn't the best choice. (In that case something from <algorithm> would be better, but keep it simple if you can.)

Glad to be of help.
Topic archived. No new replies allowed.