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

Writing to A Text file 1

Status
Not open for further replies.

MyFlight

Technical User
Feb 4, 2002
193
I am trying to write between 1 and 3 items to a file, on a certian line.

For example I open TEST.TXT (which contains a list of 5 Numbers). I want to add comments after the Numbers.

Example:

TEST.TXT

1234
1235
1236

Revised file (or a New File)

1234 Changed 123456
1235 UNCHANGED No Change
1236 Changed 123456

Code:
proc main
string szNewPswd,szSiteName,sTok1,sLine
string Fname = "Completed"
string Sname = ".txt"
sdlginput "Site Name" "Enter Site Name:" szSiteName      
set szNewPswd = "123456"
strcat Fname szSiteName 
strcat fname Sname
if isfile "C:\Temp Data Files\Raw Data\My Site.txt"
fopen 1 "C:\Temp Data Files\Raw Data\My Site.txt" READ TEXT
fopen 2 Fname CREATE
while not feof 1
   fgets 1 sLine
   strtok sTok1 sLine "," 1                 
   strreplace sLine " " " "
   fputs 2 sTok1               
   fputs 2 "CHANGED "
   fputs 2 szNewPswd
endwhile
endif
   fclose 1
   fclose 2
endproc

I know how to open the File (or Files) with fopen (READ and/or READ/WRITE). When I use the fputs or fwrite command it put the TEXT on the 1st line only.

Any Suggestions
 
You'll need to use the finsblock and/or the fseek commands to do this. Usually the easiest way to do this (other than reading from one file, massaging the data, and writing to a second file) is to read the first line using fgets, use the finsblock command to "squeeze" in the necessary number of bytes (you can find this amount by doing strlen on the string you want to insert) and then using the fputs or fwrite command to add the string to your file. An alternate method is to use the fseek command to get to the correct spot in the file, then use the finsblock and fputs/fwrite commands as before.

aspect@aspectscripting.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top