Reputation: 1863
I want to delete a record input by the user from a file if it exists. I have written the following code to add data into the file:- Could anyone tell what approach shall I use to delete a specific record(or data) form a file? Thanks!
void addData(char* name, char* data){
ifstream input_file(name);
ofstream output_file(name, ofstream::app);
if (!input_file) {
output_file << data << endl;
}
else {
output_file << data << endl;
cout << "This is the first data of this person" << endl;
}
input_file.close();
output_file.close();
}
Upvotes: 2
Views: 456
Reputation: 15275
There are basically 2 approaches.
There are also more solutions, but those 2 approaches are often used.
Please see some example codes. Just plain example. No productive code. No error checking. Just to give you an idea, how it could work.
Method 1
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
std::vector<std::string> readFile(const std::string& filename) {
// Here we will store all the data from the file
std::vector<std::string> fileData;
// Open the source file
std::ifstream fileStream(filename);
// Read line by line and add it to our fileData
std::string line;
while (std::getline(fileStream, line)) {
fileData.push_back(line);
}
return fileData;
}
void writeFile(std::vector<std::string>& fileData, const std::string& filename) {
// Open file for output
std::ofstream fileStream(filename);
// Write all data to file
for (const std::string& line : fileData)
fileStream << line << '\n';
}
int main() {
// Aproach with read complete file to local variable, modify and the store again
const std::string dataFileName("r:\\test.txt");
// Get file content
std::vector<std::string> data = readFile(dataFileName);
// Now go through all records and do something
for (std::string& line : data) {
// If some condition is met then do something, for example modify
if (line == "Line1") line += " modified";
}
// And then write the new data to the file
writeFile(data, dataFileName);
return 0;
}
Method 2:
#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
int main() {
// Aproach with temp file, remove and rename, and on the fly change
const std::string dataFileName("r:\\test.txt");
const std::string tempFileName("r:\\temp.txt");
{
// Open the source file with data
std::ifstream dataFileStream(dataFileName);
// Open the temporary file for output
std::ofstream tempFileStream(tempFileName);
// Now read the source file line by line with a simple for loop
std::string line;
while (std::getline(dataFileStream, line)) {
// Identify the line that should be deleted and do NOT write it to the temp file
if (line != "SearchString") { // Or any other condition
// Write only, if the condition is not met
tempFileStream << line << '\n';
}
}
} // The end of the scope for the streams, will call their destructor and close the files
// Now, remove and rename
std::remove(dataFileName.c_str());
std::rename(tempFileName.c_str(), dataFileName.c_str());
return 0;
}
Upvotes: 1