Hello,
So right now i`am writing a project( banking system ) and i`m stuck at deleting a line from file. Program deletes all lines after input.. ( if want to delete line 4 it doesen`t delete line 4, but deletes lines 5,6,7,etc..)
Any advice how to fix this will be really appreciated.
void del()
{
struct clientData {
int accNum;
char Sur[15];
char Name[10];
float balance;
};
int i = 0; // account number to find
int j = 0; // position counter
bool entryFound = false; //boolean to check if entry has been found
clientData client;
fstream fails;
clientData blankClient = {0, "", "", 0.0};
fails.open("credit.dat", ios::in | ios::out | ios::binary);
fails.read((char*)&client, sizeof(clientData));
do {
fails.seekp(0);
cout << "Enter account number you want to delete : (1 to 100, 0 to end input)" << endl;
cin >> i;
while (fails)
{
if ( i == client.accNum) //check if accNum from file is equal to the provided one
{
entryFound =true;
cout << "found entry to delete" << endl;
fails.seekp( j * sizeof(clientData));
fails.write((char*)&blankClient, sizeof(clientData));
break; //1st found entry will be deleted
}
fails.read((char*)&client, sizeof(clientData)); //reads data
j++;
}
if (!entryFound)
{
cout << "couldn't find entry with account number: " << i << endl;
}
} while (i != 0);
fails.close();
}
void del()
{
struct clientData {
int accNum;
char Sur[15];
char Name[10];
float balance;
};
int i = 0; // account number to find
int j = 0; // position counter
bool entryFound = false; //boolean to check if entry has been found
clientData client;
fstream fails;
clientData blankClient = {0, "", "", 0.0};
fails.open("credit.dat", ios::in | ios::out | ios::binary);
fails.read((char*)&client, sizeof(clientData));
do {
cout << "Enter account number you want to delete : (1 to 100, 0 to end input)" << endl;
cin >> i;
while (fails)
{
if ( i == client.accNum) //check if accNum from file is equal to the provided one
{
entryFound =true;
cout << "found entry to delete" << endl;
fails.seekp( j * sizeof(clientData));
fails.write((char*)&blankClient, sizeof(clientData));
break; //1st found entry will be deleted
}
fails.read((char*)&client, sizeof(clientData)); //reads data
j++;
}
if (!entryFound)
{
cout << "couldn't find entry with account number: " << i << endl;
}
} while (i != 0);
fails.close();
}