Overwrite a line in structures.

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.

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
  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();
}

i did find my mistake.. if someone needs then the right code is -
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
  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();
}
Topic archived. No new replies allowed.