deleting one char in ascii file

B

Bernhard Hidding

Hi,
my program writes chars to an ascii file via ofstream. You can use "\n" for
newline and "\t" for tab there, but is there any command that deletes the
last char in the current ofstream?
Thanks in advance,
Bernhard
 
J

John Harrison

Bernhard Hidding said:
Hi,
my program writes chars to an ascii file via ofstream. You can use "\n" for
newline and "\t" for tab there, but is there any command that deletes the
last char in the current ofstream?

No, there is a command that will move you back one position in the stream so
you can overwrite the last char with something different, but no command
that actually deletes the last char.

If you need to do this sort of thing a lot then a file is the wrong thing to
use. Do all your processing in a string (its easy to delete the last char in
a string) and only when you are finished write out the whole string to a
file.

john
 
B

Bernhard Hidding

No, there is a command that will move you back one position in the stream
so
you can overwrite the last char with something different, but no command
that actually deletes the last char.

What would that command be? I could overwrite the char with a space then.

Regards,
Bernhard
 
O

osmium

Bernhard said:
stream

What would that command be? I could overwrite the char with a space then.

seekp(). There is also a seekg(). p for output streams and g for input
streams, put and get.
 
T

tom_usenet

What would that command be? I could overwrite the char with a space then.

Assuming you're using iostreams, I think you want:

myfstream.seekp(-1, std::ios_base::cur);
//seeks back 1 char from current position

Tom
 
J

John Harrison

tom_usenet said:
Assuming you're using iostreams, I think you want:

myfstream.seekp(-1, std::ios_base::cur);
//seeks back 1 char from current position

Note this is only guaranteed to work with streams opened in binary mode

std::eek:fstream myfstream("some_file", std::ios_base::binary);

Opening a file in binary mode may have other consequences, on line endings
for instance.

john
 
B

Bernhard Hidding

Assuming you're using iostreams, I think you want:
Note this is only guaranteed to work with streams opened in binary mode

std::eek:fstream myfstream("some_file", std::ios_base::binary);

Opening a file in binary mode may have other consequences, on line endings
for instance.

Now this seems to be complicated. I am doing

ofstream scriptfile;
scriptfile.open(script.c_str(),ios::trunc);

/* here comes my ascii output */

scriptfile.seekp(-1, std::ios_base::cur);
scriptfile.close();

This works fine. However, I have opened an ascii output, not a binary
output, have I? What could go wrong when I am not in binary mode?
You know I want my code to be robust.
Regards,
Bernhard
 
M

Mike Wahler

Bernhard Hidding said:
Now this seems to be complicated. I am doing

ofstream scriptfile;
scriptfile.open(script.c_str(),ios::trunc);

/* here comes my ascii output */

scriptfile.seekp(-1, std::ios_base::cur);
scriptfile.close();

This works fine. However, I have opened an ascii output, not a binary
output, have I?

First, note that C++ does not define 'ASCII'. C++ i/o can
be done in one of two 'modes': 'text' or 'binary'.
What could go wrong when I am not in binary mode?

Unwanted character translations, or not getting a guarantee
of a given behavior which requires binary mode (such as with
'seeking'). 'Seeking' is done using 'character positions'.
On those implementations where 'text' mode translates e.g.
a '\n' from a single character to more than one (such as
Windows), this will skew the actual seek position within a
physical file.
You know I want my code to be robust.

'binary' mode will guarantee that the byte values you write will
not be modified by the OS. (whereas with 'text' mode this can
happen, e.g. with '\n'). Since all ASCII values are a subset
of the range of type 'char', 'binary' mode will preserve them
(but then you'll be responsible for ensuring that proper translation
of e.g. '\n' (if necessary) does occur.

-Mike
 
J

John Harrison

Bernhard Hidding said:
Now this seems to be complicated. I am doing

ofstream scriptfile;
scriptfile.open(script.c_str(),ios::trunc);

/* here comes my ascii output */

scriptfile.seekp(-1, std::ios_base::cur);
scriptfile.close();

This works fine. However, I have opened an ascii output, not a binary
output, have I? What could go wrong when I am not in binary mode?

Unless you open in binary mode it is not guaranteed that one character
output by you results in one character output to the file. Therefore any
attempt to count characters is dangerous. Seekp will count characters in the
file, which might not tally with the characters you have actually output.
The usual real world example is on Windows systems where outputting one
character, '\n', results in two characters, '\r' and '\n', being output to
the file.

In text mode only three types of seeks are guaranteed to work, a seek to the
beginning of the file, a seek to the end of the file, or a seek to a
position previously saved from a call to tellp (or tellg).
You know I want my code to be robust.

Good for you.

john
 

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

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,476
Latest member
blackwatermelon

Latest Threads

Top