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

How to copy string with spaces to a new file

Status
Not open for further replies.

ok1397

Technical User
Feb 7, 2005
70
0
0
US
Hello everyone. Need help with the following, i'm copying string from one file to the end of a line in another file. I need to copy string exactly the way it is on the file i'm copying from. On the line i'm copying to ends like this. WHS1 AVG=
I then write the new information:
2,018 } Which should look like this:
WHS1 AVG= 2,018 } but instead it looks like this:
WHS1 AVG=2018 }
Lines are the same length (numbers might be less for example WHS1 AVG= 300 } ) but the are the same length.

How can i write exactly the way it is to the end of the line including spaces !!!! This is not working:

strfmt sOutPut " %s" sVar4
fseek 0 (-2) 1
finsblock 0 18
fwrite 0 sOutPut 18
 
To make sure I'm understanding this correctly, you have one file where the line ends in WHS1 AVG= and another file that contains 2,018 }, correct?

It looks like the 18 in the finsblock and fwrite commands is the total length of the WHS1 AVG= 2,018 } string, but the WHS1 AVG= portion of the lines should already be in the string, so that length of the string shouldn't be calculated in the 18 character places.

Can you post your entire script, or at least all of the file operations?

 
THIS IS LINE FROM FILE 1:

File 1 (or fopen 0)

SKU21 { WHS1 AVG=

HERE'S FILE I'M COPYING FROM:

File 2

SKU106 WHS1 AVG= 1,552 }
SKU21 WHS1 AVG= 2,018 }
SKU101 WHS1 AVG= 208 }
SKU121 WHS1 AVG= 411 }
SKU321 WHS1 AVG= 2 }
SKU400 WHS1 AVG= 0 }
SKU405 WHS1 AVG= 0 }
SKU401 WHS1 AVG= 0 }

HERE'S THE CODE:

proc WHSAVG
string sLine1
string sLine2
string sVar
string sVar1
string sVar3
string sVar4
string sJAVG = "WHS1 AVG="
string sJUPAVG = "WHS1 AVG="
string sOutPut
integer iPos
integer iFound=0


fopen 0 "L:\MENUS\CODES\MERGEPC\MWINV\MWINVTEST.TXT" READWRITE TEXT
fopen 2 "L:\MENUS\CODES\MERGEPC\MWINV\WHSAVGMW.TXT" READ TEXT

fgets 0 sLine1
fgets 0 sLine1
fgets 0 sLine1
fgets 0 sLine1
fgets 0 sLine1
fgets 0 sLine1
fgets 0 sLine1
fgets 0 sLine1

while not feof 0
fgets 0 sLine1
substr sVar sLine1 0 16
sVar = StripSpaces(sVar)
strreplace sVar "," ""

if strfind sLine1 sJAVG iPos
substr sVar1 sLine1 iPos+9 0
sVar1 = StripSpaces(sVar1)
strreplace sVar1 "," ""

fgets 2 sLine2
substr sVar3 sLine2 0 16
sVar3 = StripSpaces(sVar3)
strreplace sVar3 "," ""

if strnicmp sVar sVar3 16
sVar = StripSpaces(sVar)
strreplace sVar "," ""
sVar3 = StripSpaces(sVar3)
strreplace sVar3 "," ""

;----- Below is where i write to the end of line in file 1

if strfind sLine2 sJUPAVG iPos
substr sVar4 sLine2 iPos+9 9
sVar4 = StripSpaces(sVar4)
strreplace sVar4 "," ""
strfmt sOutPut " %s" sVar4
fseek 0 (-2) 1
finsblock 0 9
fwrite 0 sOutPut 9

;----------------------------------------------------------
endif
else
rewind 0
endif

else
iFound++
endif

endwhile

fclose 0
fclose 2

endproc

func StripSpaces : string
param string targetline
integer str_length

strlen targetline str_length ;Get length of string

while str_length > 0 ;While the string has at least one character in it...
if rstrcmp targetline " " 1 ;Compare the first character in the string to a space
strdelete targetline 0 1 ;If the first character is a space, delete it from the string
else
exitwhile ;Else exit the while loop
endif
endwhile

return targetline ;Return modified string
endfunc


 
Can you post the full contents of file 1? I'm running into what looks like an endless loop when running the script with just the one line in the first file.

I did notice that the value for sVar1 always seems to be null and the comparison of sVar and sVar3 fails due to a trailing brace in sVar, so I'm not seeing any file updates.

 
Hello knob, thank you for responding and thank you for all your help. File 1 has about 6 fields, i only posted 2. I got it working and works great. I deleted the following line:

strreplace sVar4 "," ""

This was deleting the comma on 2,018 now it writes to file exactly the way i want to. Once again thank you and this forum, you all have been very helpfull !!!!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top