By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,786 Members | 1,120 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,786 IT Pros & Developers. It's quick & easy.

delete line in txt file

P: n/a
Hello all,

I want something that should be realy really simple.... but I dont get it..
searched google and stuf, but found nothing useful.

I;m opening a txt-file with a fstream. Now I want to make a function that
looks like Removeline(int Linenumber); But I can;t get it working.. anybody
has anything helpfull?

if anybody can help me that would be great :D

thnkx!!
Oct 9 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
On Oct 9, 7:43 pm, "Joah Senegal" <blo...@hva.nlwrote:
Hello all,

I want something that should be realy really simple.... but I dont get it..
searched google and stuf, but found nothing useful.

I;m opening a txt-file with a fstream. Now I want to make a function that
looks like Removeline(int Linenumber); But I can;t get it working.. anybody
has anything helpfull?

if anybody can help me that would be great :D

thnkx!!
pasting the code you are trying to make work will surely help

Oct 9 '07 #2

P: n/a
On Oct 10, 12:43 am, "Joah Senegal" <blo...@hva.nlwrote:
Hello all,

I want something that should be realy really simple.... but I dont get it..
searched google and stuf, but found nothing useful.

I;m opening a txt-file with a fstream. Now I want to make a function that
looks like Removeline(int Linenumber); But I can;t get it working.. anybody
has anything helpfull?

if anybody can help me that would be great :D
One way:

Read each line into a std::vector<std::stringand then
write that vector back out, minus the line(s) you want
removeed.

--
Chris

Oct 9 '07 #3

P: n/a
Hi Joah,
I;m opening a txt-file with a fstream. Now I want to make a function that
looks like Removeline(int Linenumber); But I can;t get it working.. anybody
has anything helpfull?
I'm not a C++ Guru, but this is the concept I would start from.

Open two file pointers p1 and p2 to the file where you want to delete
the line in. p2 is only reading while p1 is read write.

Place p1 on the line "i", where "i" is the line to delete, and p2 on the
line i+1. Read line from p2 and write it to p1. Continue like that
until you hit EOF. Fill the last line with an empty line or just EOF.

I don't know if that would work, but it is a start.

Regards
Klaas
Oct 9 '07 #4

P: n/a
On Oct 9, 4:55 pm, Klaas Vantournhout <no_valid_em...@spam.comwrote:
I'm not a C++ Guru, but this is the concept I would start from.

Open two file pointers p1 and p2 to the file where you want to delete
the line in. p2 is only reading while p1 is read write.

Place p1 on the line "i", where "i" is the line to delete, and p2 on the
line i+1. Read line from p2 and write it to p1. Continue like that
until you hit EOF. Fill the last line with an empty line or just EOF.
While that all sounds fine in theory, it won't work as expected. The
EOF marker at the end of every file is different from OS to OS. From
my understanding its 0x04 on UNIX systems, other systems use -1,
others use CTRL+Z.

So the only fool proof way is to:
-Open the file for reading, read it into a buffer minus the line.
Or more simply two buffers, one up to before the line, and the other
from the line after the deleted line, up until the end of the file.
-Close the file
-Reopen the file with the ios::create flag which should truncate the
file to a size of 0. Then write the two buffers to the file.
-Close the file, and the EOF will be written in the correct
implementation (and the code is guaranteed to be portable).

Regards,
Keith
Oct 9 '07 #5

P: n/a
Keith Halligan wrote:
On Oct 9, 4:55 pm, Klaas Vantournhout <no_valid_em...@spam.comwrote:
I'm not a C++ Guru, but this is the concept I would start from.
Open two file pointers p1 and p2 to the file where you want to delete
the line in. p2 is only reading while p1 is read write.
Place p1 on the line "i", where "i" is the line to delete, and p2 on the
line i+1. Read line from p2 and write it to p1. Continue like that
until you hit EOF. Fill the last line with an empty line or just EOF.
While that all sounds fine in theory, it won't work as expected. The
EOF marker at the end of every file is different from OS to OS. From
my understanding its 0x04 on UNIX systems, other systems use -1,
others use CTRL+Z.
There is no such thing as an EOF marker. If you create a
file, and write sequentially to it, it will contain exactly
what you have written (mapped according to the standard and
the local conventions for text or binary files, depending on
how you opened it). No more, no less. (Sort of:
officially, a binary file may contain extra bytes at the
end, and a text file extra bytes in each line. But in
practice, this doesn't occur in most modern
implementations.)
So the only fool proof way is to:
-Open the file for reading, read it into a buffer minus the line.
Or more simply two buffers, one up to before the line, and the other
from the line after the deleted line, up until the end of the file.
-Close the file
-Reopen the file with the ios::create flag which should truncate the
file to a size of 0. Then write the two buffers to the file.
-Close the file, and the EOF will be written in the correct
implementation (and the code is guaranteed to be portable).
That's a very poor way of doing it. Get a crash in the
middle, and what do you have?

The usual solution is close to what Klaas suggested, but
uses two files: open a temporary file, copy to it, skipping
whatever you don't want to keep, then close it, verify that
there were no errors when writing, delete the original
("remove()"), and rename the temporary to have the name of
the original.

--
James Kanze (GABI Software) mailto: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

Oct 10 '07 #6

P: n/a
On Wed, 10 Oct 2007 01:26:04 -0700, James Kanze wrote:
There is no such thing as an EOF marker. If you create a
file, and write sequentially to it, it will contain exactly
what you have written (mapped according to the standard and
the local conventions for text or binary files, depending on
how you opened it). No more, no less.
There was an EOF marker in early versions of MS-DOS.
I don't know if any operating system code crashed if an attempt
was made to read beyond the EOF marker in the file, but there
were some user programs that expected to find a ^Z and crashed
if it wasn't found. Naturally, they would also put it back
when saving the file.

Possible a remant from the time when files may have been stored
on a tape device without any bookkeeping, hence impossible to
know in advance how long the file is, without an EOF marker.

I would be surprised if the C++ standard did not allow such
implementation of text files.

--
Joel Yliluoma - http://bisqwit.iki.fi/
: comprehension = 1 / (2 ^ precision)
Oct 11 '07 #7

P: n/a
On 2007-10-10 21:38:15 -1000, Joel Yliluoma <bi*****@iki.fisaid:
On Wed, 10 Oct 2007 01:26:04 -0700, James Kanze wrote:
>There is no such thing as an EOF marker. If you create a
file, and write sequentially to it, it will contain exactly
what you have written (mapped according to the standard and
the local conventions for text or binary files, depending on
how you opened it). No more, no less.

There was an EOF marker in early versions of MS-DOS.
Yes, but you're mixing levels. In C and C++ you don't read an EOF
marker and you don't write one. That's the job of the IO library.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Oct 11 '07 #8

P: n/a
On Oct 9, 9:43 am, "Joah Senegal" <blo...@hva.nlwrote:
Hello all,

I want something that should be realy really simple.... but I dont get it..
searched google and stuf, but found nothing useful.
Try sed. If you want to delete 10th line of 'SomeFile.txt', simply do

sed '10d' SomeFile.txt SomeFileWithoutTenthLine.txt

- Anand

[FU-T set to c.u.s]

Oct 11 '07 #9

P: n/a
On Oct 11, 9:38 am, Joel Yliluoma <bisq...@iki.fiwrote:
On Wed, 10 Oct 2007 01:26:04 -0700, James Kanze wrote:
There is no such thing as an EOF marker. If you create a
file, and write sequentially to it, it will contain exactly
what you have written (mapped according to the standard and
the local conventions for text or binary files, depending on
how you opened it). No more, no less.
There was an EOF marker in early versions of MS-DOS.
Sort of. MS-DOS started by following many of the conventions of
CP/M, and in CP/M, a file always had a length which was a
multiple of 128 bytes. Which isn't always very convenient, so
different conventions were established. One was that a byte
with the value of 0x1A (^Z) would be considered the end of a
text file. Or that binary files had an internal structure, with
length fields, which allowed the reader to determine where they
ended.

If you read carefully the guarantees that C (and indirectly C++)
gives for text and binary files, you'll see some loopholes which
are obviously designed to allow an implementation on this OS:
when you reread a file written binary, you may read additional
bytes at the end, for example. (Other loopholes were designed
to allow text files with fixed length records.)
I don't know if any operating system code crashed if an attempt
was made to read beyond the EOF marker in the file, but there
were some user programs that expected to find a ^Z and crashed
if it wasn't found. Naturally, they would also put it back
when saving the file.
The OS wouldn't let you read past end of file, period. Whether
CP/M or MS-DOS. The problem in CP/M was that end of file could
only be at an exact multiple of 128 bytes. MS-DOS didn't have
this problem, and there was never any real need for an EOF
character in MS-DOS, but many of the earliest applications were
direct ports of CP/M programs, which in fact always did write
multiples of 128 bytes, and expected the 0x1A.

Even today, most Windows implementations of C++ will stop
reading a text file if they encounter a 0x1A in it. (I'm not
sure how this works if you attempt to seek to the end.)
Possible a remant from the time when files may have been stored
on a tape device without any bookkeeping, hence impossible to
know in advance how long the file is, without an EOF marker.
No. The EOF marker on tape wouldn't affect much; it's just an
EOF.
I would be surprised if the C++ standard did not allow such
implementation of text files.
It does.

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

Oct 11 '07 #10

P: n/a
Joah Senegal schrieb:
Hello all,

I want something that should be realy really simple.... but I dont get it..
searched google and stuf, but found nothing useful.

I;m opening a txt-file with a fstream. Now I want to make a function that
looks like Removeline(int Linenumber); But I can;t get it working.. anybody
has anything helpfull?

if anybody can help me that would be great :D
Hello,

C++ is a very low level language. You must write your own function which
does this.

--
Anton Ramunda
Contact: my******@trashmail.net
(E-Mail is protected by a Challenge-Response System)
Mar 28 '08 #11

This discussion thread is closed

Replies have been disabled for this discussion.