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!

Strings 1

Status
Not open for further replies.

PhilBreau

Technical User
Dec 14, 2001
108
CA
I know strings are limited to 256 characters. How can I read a file record which has greater than 256 characters?


some of the code:


if not fopen 0 "c:\temp.txt" READ
usermsg "can't open file"
exit
endif

while not feof 0
fgets 0 fromfile
strextract dn fromfile "|" 2
strextract firstname fromfile "|" 4
strextract lastname fromfile "|" 5
strextract costcentre fromfile "|" 7
strextract companyname fromfile "|" 9
strextract phonetype fromfile "|" 22
substr extension dn 4 4





The problem I'm having is the record from the file is greater than 256 characters. I can't extract the string phonetype from the string fromfile. This index is at position 507. How can I read further into the record beyond 256?



Thank you
 
You can use more than one fgets command in a row to read the entire record. If you use strlen to check the length of a string, you'll know you've reached the end of the record if the length is not 256. You could then parse the various strings you've read, possibly using strextract as you are now to split the values, then using strcat to put together strings that are split at the 256-byte boundary.

Another option would be to use fread to read in a certain amount of data at a time from the file. This would be useful if all of your data is padded to a certain "width" in the file. You could then use the first fread command to read in say 221 bytes to get the first four variables, then another fread to get the next five, etc. then use the strextract commands as you are now. Make sure your script accounts for any carriage returns and line feeds present at the end of each line since fread will not terminate on those characters or strip them from the read string like fopen will.


aspect@aspectscripting.com
 
I tried using the fgets command a secont time. This works great.

Thanks again knob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top