text file: how delete a character?

i have a text file fstream.
i did:
file<<"\t";
now imagine that i need delete 1 tab... how can i do it?
You can't delete characters from the middle of text files.

You can either replace it with something, by overwriting that tab character in the file with something else, or you can copy the entire file, leaving out that tab character, into a new file, then delete the old one, then rename the new one to the same name the old one used to have.
Last edited on
and change the file position for replace it?
I don't understand that question.
the 'file' have the last position on file. using '<<' operator we add more text. but we can change to the last position and then use the '<<' operator. how we can change to the last position on file?
file.seekp(ios_base::end - 1)
right?
PS: i resolved using an 'if' before print all tab's.... but that calculation is correct?
No.

ostream& seekp (streampos pos);
New absolute position within the stream (relative to the beginning).
file.seekp(file.tellp() - 1)
i see what you mean ;)
now i think it's correct
1
2
3
4
5
6
const auto old_pos = file.tellp() ;

file << '\t' ;
// etc.

file.seekp(old_pos) ; // seek to a position previously returned by tellp  
Can we also set position for input like we did with output here?
Yes; we can use tellg and seekg on input file streams.
https://en.cppreference.com/w/cpp/io/basic_istream/seekg

For file streams opened in text mode, the same restrictions apply: the only valid values for the position to seek to are zero and an earlier value returned by a successful call to tellg

JLBorges: i use a for loop for print '\t' on file(indention code)... in these case is like print 3 '\t' and go back just 1 '\t'.
1
2
3
4
5
6
7
8
9
10
11
12
13
const auto old_pos = file.tellp() ;
for(unsgined int tab=0; tab<tabcount; tab++)
         file << '\t' ;
if(word=="end")
{
     tabcount--;
}
if(word=="if")
{
    tabcount++;
}
file.seekp(file.tellp()-1) ; 
file<<something;

but i get the point and now i understand it.
ok... these code have anotherthing: if i change the 'if's' to before the 'for' loop, i avoid the position changing... is what i have changed on my code.
thank you so much for all to all
Have you considered building a string in memory first, rather than messing about tweaking the file position pointer?

1
2
3
4
5
6
7
8
ostringstream oss;
for(unsigned int tab=0; tab<tabcount; tab++)
         oss << '\t';

// more code

// When you're happy, 
file << oss.str();

You have so much more freedom to do what you want with in-memory objects.

> file.seekp(file.tellp()-1) ;
This violates what JLBorges just posted above your post.

yes.. it's more freedom ;)
"This violates what JLBorges just posted above your post."
i'm sorry what you mean? isn't correct code? or rights?
I quote.

the only valid values for the position to seek to are zero and an earlier value returned by a successful call to tellg

Subtracting 1 is no longer consistent with the restriction.
Topic archived. No new replies allowed.