473,806 Members | 2,525 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

deleting one char in ascii file

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
Jul 22 '05 #1
8 3459

"Bernhard Hidding" <hi*****@uni-duesseldorf.de> wrote in message
news:cb******** **@news1.rz.uni-duesseldorf.de. ..
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
Jul 22 '05 #2
> 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
Jul 22 '05 #3
Bernhard Hidding writes:
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.
Jul 22 '05 #4
On Tue, 29 Jun 2004 13:42:18 +0200, "Bernhard Hidding"
<hi*****@uni-duesseldorf.de> 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

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #5

"tom_usenet " <to********@hot mail.com> wrote in message
news:sn******** *************** *********@4ax.c om...
On Tue, 29 Jun 2004 13:42:18 +0200, "Bernhard Hidding"
<hi*****@uni-duesseldorf.de> 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
Jul 22 '05 #6
> > 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.seek p(-1, std::ios_base:: cur);
scriptfile.clos e();

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
Jul 22 '05 #7

"Bernhard Hidding" <hi*****@uni-duesseldorf.de> wrote in message
news:cb******** **@news1.rz.uni-duesseldorf.de. ..
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.seek p(-1, std::ios_base:: cur);
scriptfile.clos e();

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
Jul 22 '05 #8

"Bernhard Hidding" <hi*****@uni-duesseldorf.de> wrote in message
news:cb******** **@news1.rz.uni-duesseldorf.de. ..
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.seek p(-1, std::ios_base:: cur);
scriptfile.clos e();

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
Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
6648
by: Hans | last post by:
Hello, Why all C/C++ guys write: const char* str = "Hello"; or const char str = "Hello";
9
4180
by: dam_fool_2003 | last post by:
For int data type the default range starts from signed to unsigned. If we don't want negative value we can force an unsigned value. The same goes for long also. But I don't understand why we have signed char which is -256. Does it means that we can assign the same ASCII value to both signed and unsigned. That means the ASCII value can be represented with a type of signed char and also unsigned char? For example int main(void) {
9
8790
by: Magix | last post by:
Hi, char is 8 bit from -127 to 127 unsigned char is 8 bit from 0-255 from ASCII table, there is a list of char from 0-255. How char can be in negative value ? If I declare "char x", then can x hold char from 128-255 ? If want to hold value from 0-127 only?
11
2979
by: Jason Curl | last post by:
Dear C people, C90 doesn't specify if 'char' is either 'signed char' or 'unsigned char' leaving it to the implementation of the compiler. Has this changed for C99, or is it still the same? Thanks, Jason.
4
2292
by: jceddy | last post by:
Hey, I'm trying to write a file with unix-style newlines (ASCII character 10) from a c++ program on Windows...it seems that the most straightforward way to do that is just to print ( char )10, but it seems that when I try to print that to the file, the program still prints the full CR+LF Windows-style ending. The code I have looks something like this: param_file<<"BSEARCH_ALG SIMPLE"<<( char )10; Is there any way to keep the program...
12
2606
by: anonymous | last post by:
Hello, I need to replace this char  with another char. However I am not able to acieve this. I tried this but it doesnt work: str = str.Replace(chr(asc(194)), "") Can somebody help ?
6
3244
by: NormD | last post by:
I'm sending a string (xml string) to web service as a parameter. One of the tags in the xml string is the address field and the values of this tag have LF + CR chars. When I receive the string in the web service method the values have only the LF chars. What's happening here?
2
2359
by: dotnetchic | last post by:
I'm trying to parse a binary file to help me with some analysis...what I'd like to do is take a byte of data and display its ascii char value (37='%', 38='&', etc.). private void ParseFile(string fileName) { byte data; if (File.Exists(fileName)) {
4
25070
by: meendar | last post by:
Hi, I am having a character pointer which contains ascii values. i just want to convert all these ascii values to respective characters and again store it in another character pointer. Anybody please help in c language. Thanks in Advance.
0
9719
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9597
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10620
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10369
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10372
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6877
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5682
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4329
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.