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.
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)
{
}
elseif(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.
// 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>
usingnamespace 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.
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.
// 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>
usingnamespace 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.