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,

Recommended Answers

All 3 Replies

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.

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

Thanks

//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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.