Re: Which file mode to use
The way to do it is to read the data into memory, modify it in memory and then write it back out. e.g.
Code:
const char* filename = "file.txt";
std::string data;
std::ifstream infile(filename,std::ios_base::binary);
if(infile.is_open())
{
//Read data into string object and then close the file
data.assign(std::istreambuf_iterator<char>(infile), std::istreambuf_iterator<char>());
infile.close();
//print the file if you like
//std::cout << data << std::endl;
//modify data here...
//...
//write the modified data to the file
std::ofstream outfile(filename,std::ios_base::binary);
if(outfile.is_open())
{
outfile.write(data.c_str(), data.size());
infile.close();
}
else
{
std::cout << "Unable to open " << filename << " for writing!" << std::endl;
}
}
else
{
std::cout << "Unable to open " << filename << " for reading!" << std::endl;
}
Re: Which file mode to use
Thanks anyways I managed to modify the record by using the method of writing to another file and then renaming it.