473,320 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Delete chars in a file stream

Hi

How can I remove/delete the last n characters from a fstream object?

Thanks
Bernie
Jun 27 '08 #1
2 8993
On Jun 13, 10:40 am, "Bernie" <indico(at)gmx-topmail.dewrote:
How can I remove/delete the last n characters from a fstream object?
You can't.

First of all, of course, an fstream doesn't contain any
characters; it is only an interface to an external file which
contains the characters. But there's no standard way of
truncating a file, either (and some OS's don't support it at
all). The usual solution is to copy the file to a new file,
without copying what shouldn't be copied. There will often be
an OS specific means of truncating the file as well, but you
can't use it on an fstream; you'll have to check with your OS
documentation for more information there.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orient�e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S�mard, 78210 St.-Cyr-l'�cole, France, +33 (0)1 30 23 00 34
Jun 27 '08 #2
"Bernie" <indico(at)gmx-topmail.dewrote in message
news:48**********@news.bluewin.ch...
Hi

How can I remove/delete the last n characters from a fstream object?

Thanks
Bernie
[Not thoroughly tested; error checking omitted for brevity]:

#include <cstdio>
#include <fstream>
#include <ios>
#include <iostream>
#include <iterator>
#include <string>

void remove_last_n(const std::string& filename,
std::streamsize n)
{
std::string temp(std::tmpnam(0));
std::ifstream in(filename.c_str());
std::ofstream out(temp.c_str());
std::istream_iterator<charitstart(in);
std::istream_iterator<charitend;

std::iterator_traits<std::istream_iterator<char
::distance_type size(std::distance(itstart, itend));

if(size n)
{
std::streamsize new_size(size - n);
in.clear();
in.seekg(0);

for(std::streamsize i = 0; i < new_size; ++i)
out.put(in.get());

in.clear();
in.close();
out.close();
std::remove(filename.c_str());
std::rename(temp.c_str(), filename.c_str());
}
}

int main()
{
remove_last_n("test.txt", 5);
return 0;
}
-Mike

Jun 27 '08 #3

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

Similar topics

3
by: dornick | last post by:
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...
39
by: Timex | last post by:
I want to delete all comments in .c file. Size of .c file is very big. Any good idea to do this? Please show me example code.
1
by: Derrick | last post by:
I'm writing a home grown csv text file search, have sorted "id" in the first "column". Other info after that in the "row". I seek half way thru the file, get to a row boundry, determine "id" that...
3
by: Art | last post by:
What's the most efficient way to replace characters in an XML document before it is loaded into a parser? Chars I'd want to replace are in attributes and there can be N attributes, also let's...
5
by: Neo | last post by:
Hello: I am receiving a Binary File in a Request from a application. The stream which comes to me has the boundary (Something like "---------------------------39<WBR>­0C0F3E0099" without the...
2
by: TOI DAY | last post by:
Hi all, How can I delete the file on the server after the user download it? For example: I have file name "123.txt" on a server, I copy it to "ABC.txt", then allow uer download the...
16
by: lovecreatesbeauty | last post by:
/* When should we worry about the unwanted chars in input stream? Can we predicate this kind of behavior and prevent it before debugging and testing? What's the guideline for dealing with it? ...
7
by: Olaf \El Blanco\ | last post by:
/* It look for a last name... That's work OK. If 'del' is 1, after search it will delete I want to put the field valid at NOT_VALID but even fwrite return 1, it never copy anything */ void...
3
by: ujjwaltrivedi | last post by:
Can I use any method to allocate /deallocate memory other than new/ delete operators? Actually the problem is while using delete operator in my ".sqc" files the application crashes. It seems the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.