Guidance
指路人
g.yi.org
Guidance Forums / Reginald Rexx / Reginald: replace a line in a file

Register 
注册
Search 搜索
首页 
Home Home
Software
Upload

  
Forum List • Thread List • Reply • Refresh • New Topic • Search • Previous • Next First 1 Last
Message1. Reginald: replace a line in a file
#13323
Posted by: simon 2010-07-04 05:02:45
Hello together,

I wanna replace a line of a file with a new text and I used this code:

/*rexx*/

myfile = "C:\tmp\test.txt"
mytext =  "just_a_text"

line = 14

SAY LINEOUT(myfile,mytext,line) 
It works good but the problem is that all the line after the line number 14 are deleted.

Any suggestions ??

Thanks in advance.
Message2.
#13324
Posted by: PeterJ 2010-07-04 17:33:14 Last edited by: PeterJ 2010-07-04 17:39:26 (Total edited 3 times)
Assume your text file contains the following lines:

this is line 1
line 2
my line 3 

By hex viewing the file you will find that it looks like this (non hex parts):
this is line 1<crlf>line 2<crlf>my line 3<crlf>

<CRLF> is terminating each line line and is (if I remember correctly) hex: 0D0A

If you replace now Line2 by, let's say: " New Line 2", you updated file looks like this:
this is line 1<crlf>new line 2<crlf>ine 3<crlf>
Which means you have overwritten the beginning parts of line 3. Lineout is not line sensitive concerning the lenght, it just starts at the beginning of the refered line and writes the line contents, regardless the length of the old line! If you open the file in Notepad
you will see:
line 1
new line 2
ine 3

replacing lines in a text file using the LINEOUT command makes only sense if it has the same line length!
If you want a reliable update you should first read the entire file into a stem, make you updates
in the stem, and file the stem back:

LOADTEXT("MyFile.", file-name)
myfile.2="New Line 2"
LOADTEXT("MyFile.", file-name,"S")
Message3.
#13325
Posted by: simon 2010-07-13 03:55:37
THANK YOU VERY MUCH PeterJ!!!
Forum List • Thread List • Reply • Refresh • New Topic • Search • Previous • Next First 1 Last
掌柜推荐
 
 
 
 
 
 
 
 
 
 
 
 
© Tue 2024-4-16  Guidance Laboratory Inc.
Email:webmaster1g.yi.org Hits:0