Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Processing a text file

0 views
Skip to first unread message

Stephan Ceram

unread,
Mar 28, 2009, 11:02:30 AM3/28/09
to
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

Default User

unread,
Mar 28, 2009, 3:26:48 PM3/28/09
to
Stephan Ceram wrote:

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

--
Day 53 of the "no grouchy usenet posts" project

Victor Bazarov

unread,
Mar 28, 2009, 3:32:01 PM3/28/09
to
Stephan Ceram wrote:
> 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

0 new messages