Processing a text file

S

Stephan Ceram

I need to do some post-processing of a text file in my C++ application.
In detail, I have to check if the last line of the text file ends with the
string "done" or "finished". If not, the last line must be removed from
the text file. How can I achieve this?

Regards,
Stephan
 
D

Default User

Stephan said:
I need to do some post-processing of a text file in my C++
application. In detail, I have to check if the last line of the text
file ends with the string "done" or "finished". If not, the last line
must be removed from the text file. How can I achieve this?

You have three things to do.

1. Find the last line of the file.
2. Check the contents of that line.
3. Delete the line if necessary.


Now, what have you done so far? Do you know how to open a file? Read
lines of text? Search within lines?

Usually, people here won't write code from scratch for you. Post your
effort and explain where you having trouble. If you can't even start
the job, then it's beyond your capabilities and you need to fall back a
bit.



Brian
 
V

Victor Bazarov

Stephan said:
I need to do some post-processing of a text file in my C++ application.

Why do you call it "post"-processing? "Post" means "after", doesn't it?
After what?
In detail, I have to check if the last line of the text file ends with the
string "done" or "finished". If not, the last line must be removed from
the text file. How can I achieve this?

Get the size of the file. Seek to 10 characters from the end (see
"SEEK_END"), then read the 10 characters and perform the comparison from
the end backwards. If the first (the last) character isn't a '\n', then
your file ends on an incomplete line, test fails. If so, keep checking.
You should fail if you don't see 'e', 'n', 'o', 'd', '\n', or 'd',
'e', ..., 'f', '\n'.

What does this have to do with C++? Or do you not know how to seek in a
file and how to extract characters? RTFM on 'seekg' and 'get', or, if
you use 'FILE*' for your IO, see 'fseek' and 'fread'.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,108
Latest member
AlbertEste
Top