Modifying line in text file

Jun 11, 2008 at 11:21am
I have the following data stored in a text file:

LastName,FirstName,StudentNumber,PhoneNumber,Email,OHIP

They are all stored in strings.

I want to be able to (probably using classes?) have the user enter the last name of a person and be able to modify that person's data. So far, I've been able to have the user lookup a person's data and add NEW contact information. I'm thinking that I can either delete the original line and just add a new contact (which is sort of modifying... right?) for the new data; However, modifying directly would be the best... but probably a lot harder.

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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <cstdlib>
#include <iostream>
//#include <string>
#include <fstream>
//#include <sstream>
//#include <cstring>
using namespace std;


class Individual
{
      public:
             Individual(){counterA=0;counterB=0;};
             void search();
             void data();
             void display(){cout<<first<<" "<<last<<", "<<gender<<", born "<<dateOfBirth<<endl<<"Student # "<<studentNumber<<", OHIP # "<<ohip<<endl<<"Phone: "<<phone<<" Email: "<<email<<endl;};
             void store();
             char searchName[50];
             short unsigned counterA,counterB;
      private:
        
              string last, first, gender, studentNumber, dateOfBirth,ohip, phone,email;
              
};

inline void Individual::search(){
     ifstream fileA("data.txt");
    string line, found;
    char foundC[50];
    //char search[50]="Plata";
    //char line2[50];
    char *cstr, *p;
    counterA=0;          
    counterB=0;               
    while(getline(fileA, line)){
                         counterA++;//Stores number of lines DOWN from beginning
                        //will only store the FIRST set of chars before the comma into p, which is transferred to <found>
                        cstr=new char[line.size()+1];
                        strcpy(cstr, line.c_str());
                         p=strtok(cstr, ",");
                         
                         found=p;
                         //cout<<found<<endl;
                         for(int i=0; i<50; i++)foundC[i]=found[i];//converting <found> string to <foundC> char, so strcmp can work
                         if(strcmp(searchName, foundC)==0)//0 in this case represents TRUE
                                           data();//calling up member function [data]
                                           //break;
                                           
                         if(strcmp(searchName,foundC)!=0){
                         //cout<<"Was not found"<<endl;
                         counterB++;
                         //break;
                         }
                         
                         p=strtok(NULL, ",");
                         
                       
    }
    fileA.close();//closes the file so it can be reopened for other purposes
                            
                            
     
};
inline void Individual::data(){
     //cout<<"foundit"<<endl;
     string line;
     short unsigned j(0);
     char *cstr, *p;
     ifstream fileA("data.txt");
     for(int i=0;i<counterA;i++)
             getline(fileA,line);//repeats until it gets the line with the last name that matched the search in [search]
             
             //cout<<line;
             
     cstr=new char[line.size()+1];
     strcpy(cstr, line.c_str());
     p=strtok(cstr, ",");
     while(p!=NULL){
                    ++j;
                    switch(j){
                               case 1:last=p;
                               case 2:first=p;
                               case 3:gender=p;
                               if(gender=="M" || gender=="m")gender="Male";
                               if(gender=="F" || gender=="f")gender="Female";
                               case 4:studentNumber=p;
                               case 5:dateOfBirth=p;
                               case 6:ohip=p;
                               case 7:phone=p;
                               case 8:email=p;
                               p=strtok(NULL,",");
                    
                    }
     }
     fileA.close();

};
inline void Individual::store(){
     cout<<"Last name:"<<endl;
     cin>>last;
     cout<<"First name:"<<endl;
     cin>>first;
     cout<<"Gender:"<<endl;
     cin>>gender;
     cout<<"Student Number:"<<endl;
     cin>>studentNumber;
     cout<<"Date of Birth:"<<endl;
     cin>>dateOfBirth;
     cout<<"OHIP #:"<<endl;
     cin>>ohip;
     cout<<"Phone #:"<<endl;
     cin>>phone;
     cout<<"Email:"<<endl;
     cin>>email;
     ofstream fileA("data.txt", fstream::app);
     //fileA.open ("file.txt", fstream::in | fstream::out | fstream::app);
     fileA<<last<<","<<first<<","<<gender<<","<<studentNumber<<","<<dateOfBirth<<","<<ohip<<","<<phone<<","<<email<<endl;
     fileA.close();
     
};
int main(int argc, char *argv[])
{
    char yn[1];
    yn[0]='y';
    short unsigned io;
    Individual IndividualRead, IndividualWrite;
    cout<<"This program will allow you to look up the personal information of students, as well as modifying them."<<endl;
    while(yn[0]=='y' || yn[0]=='Y'){ 
                      
    cout<<"Do you want to lookup a contact or add the contents of a new contact?"<<endl<<"1\tLookup\n2\tAdd"<<endl;
    cin>>io;       
    if(io==1){
              cout<<"Enter the exact last name of the person you'd like to search for:"<<endl;
              cont:
              cin>>IndividualRead.searchName;
              IndividualRead.search();
              if(IndividualRead.counterA==IndividualRead.counterB){
                                                                  cout<<"You have entered an invalid name. Please try again. (Make sure you capitalize!)"<<endl;
                                                                  goto cont; 
                                                                  }
              
              if(IndividualRead.counterB+1==IndividualRead.counterA)IndividualRead.display();
              }
    if(io==2){
              IndividualWrite.store();
              cout<<"Your data has been stored."<<endl;
              }
    cout<<"Would you like to continue?"<<endl;
    cin>>yn;
    
}
  
    return EXIT_SUCCESS;
}
Topic archived. No new replies allowed.