C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

deleting one char in ascii file

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Bernhard Hidding
Guest





PostPosted: Tue Jun 29, 2004 10:48 am    Post subject: deleting one char in ascii file Reply with quote



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


Back to top
John Harrison
Guest





PostPosted: Tue Jun 29, 2004 11:07 am    Post subject: Re: deleting one char in ascii file Reply with quote




"Bernhard Hidding" <hidding (AT) uni-duesseldorf (DOT) de> wrote

Quote:
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



Back to top
Bernhard Hidding
Guest





PostPosted: Tue Jun 29, 2004 11:42 am    Post subject: Re: deleting one char in ascii file Reply with quote



Quote:
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



Back to top
osmium
Guest





PostPosted: Tue Jun 29, 2004 11:56 am    Post subject: Re: deleting one char in ascii file Reply with quote

Bernhard Hidding writes:

Quote:
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.

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



Back to top
tom_usenet
Guest





PostPosted: Tue Jun 29, 2004 12:07 pm    Post subject: Re: deleting one char in ascii file Reply with quote

On Tue, 29 Jun 2004 13:42:18 +0200, "Bernhard Hidding"
<hidding (AT) uni-duesseldorf (DOT) de> wrote:

Quote:
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.

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

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

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

Back to top
John Harrison
Guest





PostPosted: Tue Jun 29, 2004 12:32 pm    Post subject: Re: deleting one char in ascii file Reply with quote


"tom_usenet" <tom_usenet (AT) hotmail (DOT) com> wrote

Quote:
On Tue, 29 Jun 2004 13:42:18 +0200, "Bernhard Hidding"
[email]hidding (AT) uni-duesseldorf (DOT) de[/email]> wrote:

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.

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::ofstream myfstream("some_file", std::ios_base::binary);

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

john



Back to top
Bernhard Hidding
Guest





PostPosted: Tue Jun 29, 2004 4:02 pm    Post subject: Re: deleting one char in ascii file Reply with quote

Quote:
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::ofstream 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



Back to top
Mike Wahler
Guest





PostPosted: Tue Jun 29, 2004 4:21 pm    Post subject: Re: deleting one char in ascii file Reply with quote


"Bernhard Hidding" <hidding (AT) uni-duesseldorf (DOT) de> wrote

Quote:
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::ofstream 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?

First, note that C++ does not define 'ASCII'. C++ i/o can
be done in one of two 'modes': 'text' or 'binary'.

Quote:
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.

Quote:
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



Back to top
John Harrison
Guest





PostPosted: Tue Jun 29, 2004 6:07 pm    Post subject: Re: deleting one char in ascii file Reply with quote


"Bernhard Hidding" <hidding (AT) uni-duesseldorf (DOT) de> wrote

Quote:
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::ofstream 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?

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).

Quote:
You know I want my code to be robust.

Good for you.

john



Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.