Delete data from a data file

I wrote a code for deleting an entry from a file.
The user gives the record number recno1 which needs to be deleted. recno is the total number of records. Please help me out. I'm not able to understand what to do.

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
std::cout<<"\n DELETE A RECORD \n Enter The Record Number(1-"<<recno<<"): ";
                 std::cin>>recno1;
                 std::cout<<"Are you sure? (y/n): ";
                 std::cin>>ans;
                 if(ans=='y')
                 {
                     for(int i=1; i<=recno; i++)
                     {
                         if(i==recno1)
                         {

                         }
                         bfile>>ba_sal;
                         btemp<<ba_sal;
                         hfile>>HRA;
                         htemp<<HRA;
                         gfile>>g_sal;
                         gtemp<<g_sal;
                     }
                 }
    remove("bfile.txt");
    remove("hfile.txt");
    remove("gfile.txt");
    rename("btemp.txt", "bfile.txt");
    rename("htemp.txt", "hfile.txt");
    rename("gtemp.txt", "gfile.txt");
Last edited on
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
std::cout<<"\n DELETE A RECORD \n Enter The Record Number(1-"<<recno<<"): ";
                 std::cin>>recno1;
                 std::cout<<"Are you sure? (y/n): ";
                 std::cin>>ans;
                 if(ans=='y')
                 {std::cout<<recno;
                     for(int i=1; i<=recno; i++)
                     {
                         if(i==recno1)
                         {

                         }
                         else if(i<recno1||i>recno1)
                        {
                         bfile>>ba_sal;
                         btemp<<ba_sal<<"\n";
                         std::cout<<ba_sal;
                         hfile>>HRA;
                         htemp<<HRA<<"\n";
                         gfile>>g_sal;
                         }
                     }
    remove("bfile.txt");
    remove("hfile.txt");
    remove("gfile.txt");
    rename("btemp.txt", "bfile.txt");
    rename("htemp.txt", "hfile.txt");
    rename("gtemp.txt", "gfile.txt");
                    }


I changed it like this and only the first data entries in the three files are copied to the temp files. The original files aren't removed and the temporary files aren't renamed.
Here's one way of doing it. Not the safest or most efficient, but reasonable simple and easy to follow.

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
// Assuming you have file records.txt in the current folder
// And the contents of records.txt are:
// Joey 256
// John 128
// Raymond 342
// Lester 192
// Shimon 180

//
//

#include <iostream>
#include <string>
#include <fstream>
#include <vector>

using namespace std;

int main()
{
	vector<string> records;
	string line;
	ifstream records_input("records.txt"); // create a file stream and open the file for reading 
	while (getline(records_input, line)) // read all the lines from the file and store in vector records
	{
		records.push_back(line);
	}
	
	records_input.close(); // close the file
	size_t recno = records.size(); // get the number of lines read
	
	cout << "These are the records" << endl;
	for (size_t i = 0; i < recno; ++i) // display all the lines read
	{
		cout << i << ": " << records[i] << endl;
	}
	
	size_t recno1 = 0;
	cout << "Which record would you like to delete?" << endl;
	cin >> recno1; // get the user to pick a record
	
	if (recno1 < 0 || recno1 >= recno) // validate their choice
	{
		cout << "Invalid choice" << endl;
	}
	else
	{
		cout << "You have chosen to delete record " << recno1 << ": " << records[recno1] << endl;
		records.erase(records.begin() + recno1); // delete users choice from the records vector
		recno = records.size(); // the size of the vector will change
	}
	
	ofstream records_output("records.txt"); // create a file stream and open the file for output
	cout << "These are the records" << endl;
	for (size_t i = 0; i < recno; ++i) // write out the new records to screen
	{
		cout << i << ": " << records[i] << endl; 
		records_output << records[i] << endl;// also overwrite the original file with the new records
	}
	
	records_output.close(); // close the file
	cout << endl << "Goodbye" << endl;
	return 0;
}
Thank you for replying :D
Sorry but, I'm doing this project for school and I don't have vectors in my syllabus.
Is there any other way to do that? Please do reply if there is another way for doing it.
There are many ways, they're just not as simple.

So I can be more helpful tell me what else I've used that you're not allowed to (e.g. string, fstream, etc), and I'll accomodate those in my illustration.
I'm not allowed to use only vectors in your illustration. All others are fine :)
Last edited on
You'll get some ideas from this:

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
// Assuming you have file records.txt in the current folder
// And the contents of records.txt are:
// Joey 256
// John 128
// Raymond 342
// Lester 192
// Shimon 180

//
//

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
	string line;
	ifstream records_input("records.txt"); // create a file stream and open the file for reading 
	size_t recno = 0;
	while (getline(records_input, line)) // count all the lines from the file
	{
		++recno;
	}
	
	string *records = new string[recno]; // Dynamically allocated array for holding strings
	
	// we're now past the end of the file, in this case, I think it's simpler to close the file and start afresh
	records_input.close(); // close the file
	records_input.open("records.txt"); // open the file
	for (size_t i = 0; getline(records_input, line); ++i) // read all the lines from the file and store in records array
	{
		cout << line << endl;
		records[i] = line;
	}
	
	records_input.close(); // close the file
	
	cout << "These are the records" << endl;
	for (size_t i = 0; i < recno; ++i) // display all the lines read
	{
		cout << i << ": " << records[i] << endl;
	}
	
	size_t recno1 = 0;
	cout << "Which record would you like to delete?" << endl;
	cin >> recno1; // get the user to pick a record
	
	if (recno1 < 0 || recno1 >= recno) // validate their choice
	{
		cout << "Invalid choice" << endl;
	}
	else
	{
		cout << "You have chosen to delete record " << recno1 << ": " << records[recno1] << endl;
		records[recno1].erase(); // blank out users choice 
	}
	
	ofstream records_output("records.txt"); // create a file stream and open the file for output
	cout << "These are the records" << endl;
	for (size_t i = 0; i < recno; ++i) // write out the new records to screen
	{
		if (!records[i].empty())
		{
			cout << i << ": " << records[i] << endl; 
			records_output << records[i] << endl;// also overwrite the original file with the new records
		}
	}
	
	records_output.close(); // close the file
	cout << endl << "Goodbye" << endl;
	return 0;
}


In summary, we read all the lines from the file, once the user selects, we convert the record he selected to an empty string, and then we overwrite the original file, using only the non-empty strings.
Thank you very much :D :D
Topic archived. No new replies allowed.