Check if ID exists in file

Hi guys,
I'm writing code for an appointment planner which is saved in a .dat file which saves each appt as follows.
15; John Brook; 25; 2; 2011; 11; 50; Home;
ID; Name; Day, Month, Year, StartTime, Duration, Location
It needs to be able to make new appointments and modify old appointments.

I need to write an error check to see if the ID entered exists within the data file. I was thinking about writing a vector and pushing back each ID into an element of the vector, but I could not figure out how to use this to error check what I need to.

I had to cut out some code due to the constraints, but this should be everything necessary.
Thanks for the help in advanced.
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
void Modify_Appointment(ifstream& in_app_file, ofstream& out_app_file)
{
    string appointmentID2;
    cout << "Enter the appointment ID that you would like to modify"<<endl;
    cin >> appointmentID2;

    string line;
    while (in_app_file.good())
    {
        getline(in_app_file, line);
        cout << line << endl;
        int appointmentIDLength = appointmentID2.length();
        string IDCheck = appointmentID2 + ";";
        if (line.substr(0, appointmentIDLength + 1) == IDCheck)
        {
            int count_appointmentID = 0;
            while (line[count_appointmentID] != ';')
            {
                count_appointmentID++;
            }
            
          /* cut out counter code here */
		
            cout << "Please select from one of the following:"<<endl<<"W-to modify 'With' attribute"<<endl<<"D-to modify the 'Date' attribute"<<endl<<"T-to modify the 'timing' attribute"<<endl<<"L-to modify the 'location' attribute"<<endl;
            string C_selection_screen;
            cin >> C_selection_screen;

            if (C_selection_screen == "W")
            {
                string appointmentWith2;
                cout << "Enter the new name:"<<endl;
                cin.ignore(1, 'n');
                getline(cin, appointmentWith2);
				//string name = line.substr(count_appointmentID, count_appointmentWith - count_appointmentID);
				//string day =
				cout << out_app_file.good() << endl;
				cout << out_app_file.fail() << endl;
				 cout << line.substr(0, count_appointmentID) << "; "<< appointmentWith2 << 
                    line.substr(count_appointmentWith, count_day - count_appointmentWith) <<
                    line.substr(count_day, count_month - count_day) <<
                    line.substr(count_month, count_year - count_month) <<
                    line.substr(count_year, count_appointmentStartTime - count_year) <<
                    line.substr(count_appointmentStartTime, count_appointmentDuration - count_appointmentStartTime) <<
                    line.substr(count_appointmentDuration, count_appointmentLocation - count_appointmentDuration) << endl;
                out_app_file << line.substr(0, count_appointmentID) << "; "<< appointmentWith2 << 
                    line.substr(count_appointmentWith, count_day - count_appointmentWith) <<
                    line.substr(count_day, count_month - count_day) <<
                    line.substr(count_month, count_year - count_month) <<
                    line.substr(count_year, count_appointmentStartTime - count_year) <<
                    line.substr(count_appointmentStartTime, count_appointmentDuration - count_appointmentStartTime) <<
                    line.substr(count_appointmentDuration, count_appointmentLocation - count_appointmentDuration) << endl;
				
            }
            else if (C_selection_screen == "D")
            {
                string day2;
                string month2;
                string year2;

                cout  << "Enter new appointment day of month:"<<endl;
                cin >> day2;
                cout << "Enter new appointment month of year:"<<endl;
                cin >> month2;            
                cout << "Enter new appointment year:"<<endl;
                cin >> year2;
                    
                out_app_file << line.substr(0, count_appointmentID) <<
                    line.substr(count_appointmentID, count_appointmentWith - count_appointmentID) <<
                    "; " << day2 << "; " << month2 << "; " << year2 <<
                    line.substr(count_year, count_appointmentStartTime - count_year) <<
                    line.substr(count_appointmentStartTime, count_appointmentDuration - count_appointmentStartTime) <<
                    line.substr(count_appointmentDuration, count_appointmentLocation - count_appointmentDuration) << endl;
            }
            else if (C_selection_screen == "T")
            {
                string appointmentStartTime2;
                cout << "Enter the new start time:"<<endl;
                cin >> appointmentStartTime2;

                out_app_file << line.substr(0, count_appointmentID) <<
                    line.substr(count_appointmentID, count_appointmentWith - count_appointmentID) <<
                    line.substr(count_appointmentWith, count_day - count_appointmentWith) <<
                    line.substr(count_day, count_month - count_day) <<
                    line.substr(count_month, count_year - count_month) <<
                    "; " << appointmentStartTime2 <<
                    line.substr(count_appointmentStartTime, count_appointmentDuration - count_appointmentStartTime) <<
                    line.substr(count_appointmentDuration, count_appointmentLocation - count_appointmentDuration) << endl;

                cout << "Would you like to change the duration as well? Enter 1 for yes, 0 for no"<<endl;
                bool duration_change;
                cin >> duration_change;

                if (duration_change==1)
                {
                    string appointmentDuration2;
                    cout << "Enter new duration:"<<endl;
                    cin >> appointmentDuration2;

                    out_app_file << line.substr(0, count_appointmentID) <<
                    line.substr(count_appointmentID, count_appointmentWith - count_appointmentID) <<
                    line.substr(count_appointmentWith, count_day - count_appointmentWith) <<
                    line.substr(count_day, count_month - count_day) <<
                    line.substr(count_month, count_year - count_month) <<
                    line.substr(count_year, count_appointmentStartTime - count_year) <<
                    "; " << appointmentDuration2 <<
                    line.substr(count_appointmentDuration, count_appointmentLocation - count_appointmentDuration) << endl;
                }
                else {}
            }
            else if (C_selection_screen == "L")
            {
                string appointmentLocation2;
                cout << "Enter the new location:" << endl;
                cin >> appointmentLocation2;

                out_app_file << line.substr(0, count_appointmentID) <<
                    line.substr(count_appointmentID, count_appointmentWith - count_appointmentID) <<
                    line.substr(count_appointmentWith, count_day - count_appointmentWith) <<
                    line.substr(count_day, count_month - count_day) <<
                    line.substr(count_month, count_year - count_month) <<
                    line.substr(count_year, count_appointmentStartTime - count_year) <<
                    line.substr(count_appointmentStartTime, count_appointmentDuration - count_appointmentStartTime) <<
                    "; " << appointmentLocation2 << endl;

            }
        }
        else 
        {
            out_app_file << line << endl;
        }

		
    }

I wrote this function some time ago for a similar problem, You will need to include the appropriate STL header files.
You could add the following bool function which will enable you to search the database file. It will return TRUE if the name
entered exists or FALSE if the name is not found.

You would call it in your program in the following manner:

bool Status = Strstr(databasefile,name2find);

if (Status) {
// name found so do your things here
} else {
// name not in database
// continue with program
}

bool Strstr(const string &InFileName,const string &SrchFileName) {
vector<string>::iterator itr;
typedef istream_iterator<string> str_input;
vector<string> vec;
string word;


word = SrchFileName;
ifstream fin(InFileName.c_str());

if (fin.fail()) {
std::cerr << "Unable to open " << InFileName.c_str() << std::endl;
exit(1); // <cstdlib>
}

copy(str_input(fin),str_input(),back_inserter(vec));
sort(vec.begin(),vec.end());

if (!fin.eof()) {
fin.close();
std::cerr << "Read error in " << InFileName.c_str() << std::endl;
exit(1);
}
fin.close();
bool Find_Word = false;
Find_Word = binary_search(vec.begin(),vec.end(),word);

return Find_Word;
}
/*
The previous solution I sent you uses a binary search to find the particular word in
a file. As I failed to comment it fully here is a second version with comments.
Note that in both programs the search will only find One Word. So if you intend
to search for John Smith or Jack Smith you will need to place an underscore character
in your data base file such as John_Smith and Jack_Smith. Then both of these solutions
will find either name when entered in a similar manner at a command prompt.
*/
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>

using namespace std;

int main(){

cout << "\tEnter the name of the file to be searched: ";
string SrcFileName;
cin >> SrcFileName;
cin.get(); // clear buffer
ifstream fin(SrcFileName.c_str()); // open file

if (fin.fail()) {
cerr << "\tError: Failed to open " << SrcFileName.c_str() << endl;
exit (1);
}

vector<string>::iterator ivec; // iterator string vector
typedef istream_iterator<string> input_stream; // declare a input stream iterator.
vector<string>vec,invec; // create two string vectors.
// copy the contents of file into a vector of strings
copy(input_stream(fin),input_stream(),back_inserter(vec));
fin.close(); // and close the file

cout << "\tEnter the Name to search for: ";
// load name to search for into a vector of strings
for (input_stream fname(cin); fname != input_stream(); fname++){
invec.push_back(*fname);
break;
}

// search vector(File) for the vector(Name)
ivec = search(vec.begin(),vec.end(),invec.begin(),invec.end());


if (ivec != vec.end()) { // if we didn't reach end then we found it
cout << *ivec << " Name exists in " << SrcFileName << endl;
// do your stuff here
} else {
cout << "Name not found in " << SrcFileName << endl;
// do your stuff here
}

return 0;
}
Topic archived. No new replies allowed.