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
|
void POS::deleteRecord(char fileName[], string itemNum)
{
ofstream write;
ifstream read;
counter = 0;
string num, name, manu, type, reso, cost, pric, quan;
read.open(fileName);
write.open("temp.txt", ios::out);
write << ""; // empty the file just in case;
getline(read, num);
getline(read, name);
getline(read, manu);
getline(read, type);
getline(read, reso);
getline(read, cost);
getline(read, pric);
getline(read, quan);
getline(read, readSpace);
while (!read.fail())
{
if (itemNum.compare(num)== 0) // is item number == to our search item?
{
num = "";
name = "";
manu = "";
type = "";
reso = "";
cost = "";
pric = "";
quan = "";
readSpace = "";
continue; // skip and move to the next record
}
else
{
counter++;
// store only valid records (quantity > 0)
write << counter << endl
<< name << endl
<< manu << endl
<< type << endl
<< reso << endl
<< cost << endl
<< pric << endl
<< quan << endl
<< endl;
}// end else
getline(read, num);
getline(read, name);
getline(read, manu);
getline(read, type);
getline(read, reso);
getline(read, cost);
getline(read, pric);
getline(read, quan);
getline(read, readSpace);
}// end while
read.close();
write.close();
// delete the original file
remove (fileName);
int result;
// rename the temp file to original name
result = rename("temp.txt", "Printers.txt");
if (result == 0)
cout << " Successfully!";
else
cout << " Unsuccessful!";
remove("temp.txt");
// reset counters
counter = 0;
}
|