Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete a line from a text file 3

Status
Not open for further replies.

Mpking

Technical User
Mar 2, 2002
23
US
I'm creating a script that will build phones in a Meridian Phone system based on a CSV file. I would have a second file that would only contain the TN for each phone.

What has gotten me stuck is that I cannot find an easy way to delete a line from the top of the file.

I want to read a line, (I'm using fgets) and after my function that proccess that data ends, i want to delete that line. (So it won't be used again, it is being copied to a existing phone file as part of my function)

Any thoughts? I could post the whole code of what I'm working on, but I felt the question would be less confusing if I told you what I was trying to accomplish.

Mike King
mking@bridgew.edu
 
Mike,

If you want to Delete only the First Line in a Text File, that should not be a Problem.
Procomm Plus 4.8 can handle that easily, if your file is Stuctured. ( Stays Constant )
You'll need to know the Number of Characters PLUS the LF/CR on Windows Platform.

Your Script Would Look Someting Like This:

#define _RECSIZE 10 ; 10 CHARS Per Line w/ LF/CR

proc DEL_LINE_1

fseek 1 0 0 ; Start of File
fdelblock 1 _RECSIZE ; Delete Line string

endproc

Let Me know if you need any further Help

Hank

 
Unfortunatly, I thought of that. However, since I will be dealing with names, the First and Last names will be varible.

My only thoughts at this point are

The hard way:

Read the line from file 0.
Imediatly copy file 0 to a new file, file 1 till you reach EOF
close file 0
close file 1
copy new file to old file.


I think it would work, just lots of file copying, not sure how that will affect Memory/script processing.

Looking for a more elegant solution, that will deal with varible lines.

Mike
 
Mike,

There should be a Solution here.. I'll play with it and get Back...

Happy and Safe 4th of July !!

Hank
 
Here's a script that should do the trick for you. I did a bit of error checking in it as well that may not be necessary for your particular situation.

proc main
string sToDelete
integer iLen

if fopen 0 "c:\program files\procomm plus\download\test.txt" READWRITE
fgets 0 sToDelete
if success
strlen sToDelete iLen
rewind 0
if success
fdelblock 0 iLen
if failure
usermsg "Print a warning message"
endif
endif
endif
fclose 0
endif
endproc
 
Mike,

Did This Little Trick and it worked Fine no matter how many Characters were on the Line.

proc Main
string sLine
fopen 0 "C:\Files\Test.txt" READ TEXT
fopen 1 "C:\Files\Test1.txt" CREATE TEXT
fgets 0 sLine
while not feof 0
fgets 0 sLine
fputs 1 sLine
endwhile
fclose 0
fclose 1
endproc

Hank
 
Hmm knob, that's very slick. I don't have procomm on this machine, but would that leave blanklines in the beginning of the file?

I know this is the first thing I'm gonna try tommrrow.

As for the second method,

I suppose if I put the file copy off till I'm about to finish the script, instead of every iteration, it wouldn't be that much of disk write/memory hog.

It's still something I'm considering, and I can't make the first one fit my code, This will be the one that I think I could make work.
 
No, it will take out the entire first line, including any trailing carriage return and linefeed. I tested this on a text file I had laying around and it removed the first line (including carriage return) that was read in with the fgets command. Now, if the first line was blank (basically your file started with a carriage return or linefeed), then all my example would do is remove the CR or LF.
 
Very cool, very elegant.

Tommorrow I'll give that a shot twisting it too my needs..

I think this might solve one of my other problems, removing a line from a file in the middle.

Just work a findfirst statement in with my primary key value, and it will work.

Thanks Knob, you've probably just saved me 20 bucks in asprine. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top