473,395 Members | 1,696 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,395 software developers and data experts.

delete line in txt file

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
10 12191
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that...
5
by: ad | last post by:
I want to delete some text which begin with <!DOCTYPE and end with > in a file. How can I do it?
6
by: I am Sam | last post by:
I keep getting this error and I don't know why: The path is too long after being fully qualified. Make sure path is less than 260 characters. Description: An unhandled exception occurred...
6
by: santa19992000 | last post by:
I have a file called xyz.txt, if the first word matches, I have to delete the whole line, how can I do that. sample is below file xyz.txt ======= SP 0/23/2345/345 CL 00/34/3456/54 In the...
3
by: silver360 | last post by:
Hello, I'm trying to create a basic Heap manager and i have some question about new/delete overloading. The following code give me this output : >> $./heap >> registered : 0x804d098 >>...
9
by: groleo | last post by:
Hi list. Simple question: is it possible to override the global new/delete operators, without using malloc/free? I mean something in the ideea of the code below, which doesnt work cause of...
9
by: Juergen Huber | last post by:
hello, i am a dummy user in python and new in this programming language and so there will be questions, that´s for you no problem! i never have before programmed in any language! sorry for this!...
4
by: jeepers | last post by:
Hi all, my problem is delete a line in a text file. the file contain -n line with user, userCode. i want to replace the line with user is egual at user, and/or insert new line but delete the...
2
by: Francesco Pietra | last post by:
Please, how to adapt the following script (to delete blank lines) to delete lines containing a specific word, or words? f=open("output.pdb", "r") for line in f: line=line.rstrip() if line:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.