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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

deleting a string in the middle of a file

Status
Not open for further replies.

Mpking

Technical User
Mar 2, 2002
23
0
0
US
Hello all,

I'm trying to add a section of code to a script I have written.

the script I have looks through a comma seperated file for a certian (unique) value, when it find that value, do some processing on that value. I need to add a section to then delete it from the file. (Unlike my last question, this line appears anywhere in the file)

At the point that I'm at, i've already read the line, so the file pointer is at the next line.

Here is the bit of code i've come up with:

Assumptions
savestring is the line I read (the file I'm trying to delete the lines out of was opened READWRITE TEXT)
iLen is a Interger
the two files exist, and (for test purposes)the same number of lines exist in each. There are 3 lines in each file. When the scrip finishes execution, there should be no lines in file ID 4.

file ID 4 contains this at start of execution
8005,156 0 0 1
8006,156 0 0 2
8007,156 0 0 3

The following code is called when my all of my processing has finished based on the info it found.

Code:
strlen savestring iLen
		
		ftell 4 fileposition
		fileposition=fileposition-iLen
		fseek 4 fileposition 1
		if success
		  fdelblock 4 iLen
		  if failure
			strfmt DEBUG_MSG "ERROR DELETING TN!!"
			fputs 2 DEBUG_MSG
		  endif
		endif

Of course, this doesn't work the way I want it to. I highly suspect it's because I'm subtracting a integer from a long.

The results I get are as follows.

Remember the two files had the same lines in them, so one should be empty when this is executed, but I have this instead. (the format of the file is two comma separated feilds, it appears that one character of one line is left over, and one entire line is also leftover, and appended to that leftover character) I am assuming it is the first character from the first line, and that is the complete 3rd line.

88007,156 0 0 3

That being said... Ummm help? (sorry for the lenght, I just wanted to provide all the info I've got)
 
Hello,

I threw this together and maybe it will help. This Script searches the TestFile for 8004, and thing Writes it to the Screen and then Deletes it from the File. This only Work if all Lines are the Same Length; but you may be able to Play around with it.

TestFile:

8005,156 0 0 1
8006,156 0 0 2
8007,156 0 0 3
8009,156 0 0 4
8004,156 0 0 5
8003,156 0 0 6
8002,156 0 0 7
8001,156 0 0 8

Script:

Proc Main

#define _RECSIZE 16
string sLine
integer Cnt
Long Addr

fopen 1 "C:\Files\TestFile.txt" ReadWrite Text
while not feof 1
fgets 1 sLine
Cnt++
if strfind sLine "8004"
termwrites sLine
fseek 1 0 0
Addr = (Cnt * _RECSIZE - _RECSIZE)
fseek 1 Addr 1
fdelblock 1 _RECSIZE
endif
endwhile
endproc

Hank
 
I'm not sure if this will help. The second field can varible as that it counts up to 15's. IE 156 0 0 1 thru 156 0 15 15.

However, I noticed you defined the size at 16. I count only 14 charaters. Are you also counting the %n or /n (I forget) that terminates a string?

Could this be why my attempt was failing?

Mike
 
Yes,

CR/LF need to be Counted to Make up a Total of 16 in my Example.

I'll Change the TestFile for Extended Characters and Change the Script. I had a Feeling that you might encounter this so I'll see what I can come up with. It might take counting the Characters on each Line Until you reach the Line You're Looking For and then backing up.

What's the Range of the Data Line ??

Example: 156 0 0 0 -> 156 0 99 99

Hank
 
Hello Again,

I think this will get you going, with the Variable Characters per Line.

TestFile:

156 0 0 0
156 0 0 1
156 0 0 2
155 0 18 19
155 0 18 20
156 0 0 3
156 0 0 4
156 0 99 99
156 0 0 5
156 0 0 6

Script:

Proc Main

string sLine
integer Total
integer Found
integer Len
Long LTotal

fopen 1 "C:\Files\TestVar.txt" ReadWrite Text
while not feof 1
fgets 1 sLine
strlen sLine Len
Total = Total + Len + 2
if strfind sLine "156 0 99 99"
strlen sLine Len
Found = Len + 2
Total = Total - Found
LTotal = Total
termwrites sLine
fseek 1 0 0
#define _RECSIZE Found
fseek 1 LTotal 1
fdelblock 1 _RECSIZE
endif
endwhile
endproc

Let Me Know.

Hank
 
It looks like the problem in your script was in the fseek 4 fileposition 1 line. Since fileposition was where you wanted to be, the last argument should be zero (seek from origin) rather than 1 (seek from current pointer position). When I made this change in your script, the line I expected to be deleted from the file was. Here is the test script I was using:

proc main
integer iLen
long fileposition
string DEBUG_MSG, savestring

fopen 4 "test.txt" READWRITE
while not feof 4
fgets 4 savestring
strlen savestring iLen
if rstrcmp savestring "800" 3
ftell 4 fileposition
fileposition=fileposition-iLen
fseek 4 fileposition 0
if success
fdelblock 4 iLen
if failure
strfmt DEBUG_MSG "ERROR DELETING TN!!"
fputs 2 DEBUG_MSG
endif
endif
endif
endwhile
fclose 4
endproc
 
Thanks guys.

I made two mistakes in my code, (The perils of coding at 2am after a 14 hour day at work)

One was the seek from current position instead of from start of file.

The other was I was using the wrong varible for my string to delete. savestring should have been another varible that I never mentioned. savestring was another string that I was saving for debug messages.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top