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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
#include <iostream> // Need for cout,cin
#include<iomanip> // Need setf,fixed,showpoint,setprecision
#include <stdio.h> // Need for getchar
#include <fstream> // Needed for files
#include <cstdlib> // Needed for exit function
#include <string> // Need for string class
using namespace std;
string getInputFileName(); // a function to prompt for the complete file name
int getId(); // Function that gets the id number
void search(ofstream& outFile, ifstream& inFile, int);
int main ()
{
ofstream outFile;
ifstream inFile;
string fileName; // complete file name including the path
fileName = getInputFileName(); // prompt and obtain the full file name
// try to open the file
outFile.open(fileName.c_str(),ios::in | ios::out | ios::app);
inFile.open(fileName.c_str(),ios::in);
if (!inFile.is_open())
{
cerr << "File open error " ;
cout << " Press enter to continue" << endl;
cin.ignore();
char ch = getchar();
exit (1);
}
if(!outFile.is_open())
{
cerr << "File open Error Creating file" ;
cout << " Press enter to continue" << endl;
cin.ignore();
char ch = getchar();
exit(1);
}
/*
char c;
inFile.get (c);
while (!inFile.eof ())
{
cout << c;
inFile.get (c);
}
string str;
int id_num;
while (inFile >> str >> id_num)
{ cout << str << "ID" << id_num << endl;
}
*/
int ID;
ID = getId();
search(outFile, inFile, ID);
outFile.close();
inFile.close();
cout.setf (ios::showpoint );
cout.setf( ios::fixed);
cout << setprecision(2);
cout << " Press Enter to continue" << endl;
cin.ignore();
char ch = getchar();
return 0;
}
//************************************************************
//
// Function name: getInputFileName
//
// Purpose: to prompt for the fully qualified name of a file
// i.e. including the path of the file
//
// Input parameters: none
//
// Output parameters: none
//
// Return Value: a string containing the fully qualified name
// of a file
//
//************************************************************
string getInputFileName()
{
string f_Name; // fully qualified name of the file
cout << "Please enter the fully qualified name of the " << endl
<< "input text file (i.e. including the path): ";
cin >> f_Name ;
cout << endl; // skip a line
return f_Name;
}
//************************************************************
//
// Function name: getId
//
// Purpose: to prompt the user for the ID
//
//
// Input parameters: char id
//
// Output parameters: none
//
// Return Value: inputId
//
//
//************************************************************
int getId ()
{
int inputId;
cout << " Enter the ID number: " ;
cin >> inputId ;
return inputId;
}
//************************************************************
//
// Function name: Search
//
// Purpose: Locate the ID the user entered. If found displays it with the name if not asks to enter into to the file
//
//
// Input parameters: string id, filehandle passed by reference (ifstream &inFile, ofstream &outFile)
//
// Output parameters: none
//
// Return Value:
//
//
//************************************************************
void search(ofstream &outFile, ifstream &inFile, int ID)
{
int id_num;
string name;
char userreply;
char lookagain = 'Y';
string inputname;
while (!inFile.eof() && (lookagain == 'Y' || lookagain == 'y'))
{
inFile.seekg (0, ios::beg);
inFile >> id_num >> name;
if ( ID == id_num)
{
outFile << id_num << name;
}
else
{
cout << "Id not found would you like to add it? Y/N " ;
cin >> userreply;
if( userreply == 'Y' || userreply == 'y')
{
cout << " What name would you like to input?" ;
cin >> inputname;
outFile << "\n" << ID << " " << inputname;
outFile.flush();
}
}
cout << "Look for another ID? Y/N " ;
cin >> lookagain;
if (lookagain == 'Y' || lookagain == 'y')
{
inFile.clear();
int getId ();
}
else
{
break;
}
}
}
| |