0

Hi
I need some help in writing a c++ program that will read a file and delete a specified number of lines e.g. say i have data file with numbers 1 to 100 i.e.
1
2
3....
100
say i want to delete top ten lines and then save the file.

Many thanks,

3
Contributors
3
Replies
7
Views
8 Years
Discussion Span
Last Post by firstPerson
0

In order to delete lines you have to completly rewrite the file, omitting the lines you want to delete

1. open the input file for reading
2. open a temp file for writing
3. start a loop that reads a line from input file. If it is not one of the lines to be deleted then write it out to the output file.
4. After the loop finishes, close both files.
5. Delete the original file
6. rename the temp file to the name of the original file.

0

Can you please write down a small code as an example.

Thanks

0
//open a file for for reading and writin
ofstream oFile("outputFile.txt");
ifstream iFile("inputFile.txt");
char c;
std::string str;
while(iFile.get(c)) { str +=c; } //read in data

//now write into the output file

Edited by Nick Evan: Fixed formatting

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.