replacing chars in a text file

D

dornick

So I want to do the above, and I really, REALLY don't want to rewrite
the entire file. I've been working on it for a while now, and can't
for the life of me get it functioning. Basically, I want to replace
the last text character of a certain line.

So far all I've done has centered around trying to put the "put"
pointer write before the character to write (ban pun, I know). But
when I tried to use put(), nothing happened and a call to bad()
afterward revealed the stream to be messed up. The spot I could get it
to work was when I did no file reading from the start. So I tried
making a new stream for output only, and it just erased the file. Argh!

If any of you guys could help me, I'd be very grateful, and would...
well... say thank you.
 
D

Dietmar Kuehl

dornick said:
So far all I've done has centered around trying to put the "put"
pointer write before the character to write (ban pun, I know). But
when I tried to use put(), nothing happened and a call to bad()
afterward revealed the stream to be messed up.

Streams (similar to C files) have an underlying state they are in.
Switching from one state to another is only possible with an
intervening "seek", even if the seek does not reposition to the
current file location. It is somewhat unfortunate that this
behavior is only rarely documented. I know that the I/O documentation
by P.J.Plauger/Dinkumware describes this, as does Nicolai Josuttis
book on the standard C++ library. However, the latter hides it away
in an inconspicuous paragraph (in Section 13.9) while the other
documentation has neat state diagram.
So I tried
making a new stream for output only, and it just erased the file.
Argh!

This so to be expected! You might want to look at the documentation
more closely...

Personally, I don't play too many tricks with streams: I use them
to read OR to write. I don't attempt to update individual bytes. I
don't think that IOStreams are really suitable to layer some
persistance mechanism upon which reads and writes the same file.
This is what mmap(2) (or its equivalent on non-POSIX systems) is
good for.
 
D

dornick

Dietmar said:
Streams (similar to C files) have an underlying state they are in.
Switching from one state to another is only possible with an
intervening "seek", even if the seek does not reposition to the
current file location. It is somewhat unfortunate that this
behavior is only rarely documented. I know that the I/O documentation
by P.J.Plauger/Dinkumware describes this, as does Nicolai Josuttis
book on the standard C++ library. However, the latter hides it away
in an inconspicuous paragraph (in Section 13.9) while the other
documentation has neat state diagram.

I am currently in a foreign country and do not have access to any paper
reference on the standard library. While I appreciate your answer, I
am somewhat confused by it. What exactly do you mean by an
"intervening seek" and what effect does this have on the stream?
Argh!

This so to be expected! You might want to look at the documentation
more closely...

Personally, I don't play too many tricks with streams: I use them
to read OR to write. I don't attempt to update individual bytes. I
don't think that IOStreams are really suitable to layer some
persistance mechanism upon which reads and writes the same file.
This is what mmap(2) (or its equivalent on non-POSIX systems) is
good for.

So what is your suggestion? Is the mmap part of the standard library,
or is it separate? I would appreciate any manner of replacing
individual characters without rewriting the entire file.
 
D

Dietmar Kuehl

dornick said:
I am currently in a foreign country and do not have access to any paper
reference on the standard library.

So what? Go and seek electronic documentation! You can go to
<http://www.dinkumware.com/>, seek the C (note the absence of a
"++") library reference, go to the "Files and Streams" section,
and find the state diagram relatively close to the buttom. This
diagram includes state transitions labeled "p" for "positioning",
i.e. seeking. Only the drawn arrows form legal state transitions.
Since you cannot go from "reading" to "writing" without going
through the base state (probably "byte oriented"), unless you
happen to be at the end of your file which would result in
appending rather than replacement, you need to position, i.e.
seek, between reading and writing.
While I appreciate your answer, I
am somewhat confused by it. What exactly do you mean by an
"intervening seek" and what effect does this have on the stream?

How does it sound? At the one end you read, at the other end you
write, inbetween you need to seek. You should read up on the
stream's 'seekp()' member functions (and before you ask:
Dinkumware also has C++ library documentation; the Dinkumware
documentation is well worth its purchase).
So what is your suggestion?

I don't know your needs! You asked for an approach replacing one
character in a file. I can conceive many reasons to do so and I
would use rather different techniques to address them, some of
them involving a database, platform specific approaches as
mmap(2), or rewriting a file. Personally, I would probably stay
clear of changing files through IOStreams because this is not
really what they are supposed to be used for. Also, I have
implemented this library and thus got an idea what is going on
inside: I would rather use mmap(2) although the particular calls
are environment specific.
Is the mmap part of the standard library,

Well, the comment in parenthesis should have made it clear that
mmap(2) is part of the POSIX standard but not of the C++ standard.
I would appreciate any manner of replacing
individual characters without rewriting the entire file.

.... and I had provided several pointers to investigate, already
in my first reply.
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top