delete line in txt file

J

Joah Senegal

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

ravinder thakur

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
 
C

Chris ( Val )

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::string> and then
write that vector back out, minus the line(s) you want
removeed.
 
K

Klaas Vantournhout

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
 
K

Keith Halligan

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
 
J

James Kanze

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

Joel Yliluoma

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

Pete Becker

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

Anand Hariharan

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

James Kanze

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

Anton Ramunda

Joah said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,498
Latest member
log5Sshell/alfa5

Latest Threads

Top