Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Why one code works, while other doesn't?
Message
 
 
To
24/01/2001 12:47:51
Dragan Nedeljkovich (Online)
Now officially retired
Zrenjanin, Serbia
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00466654
Message ID:
00468046
Views:
39
Hi Dragan,

I got really weird results:
set safety off
local inFile, outFile, lcBuffer, lcFileName, lcSendFileName, lnSec
lcFileName="d:\redp\output\customer\website\Cprc0042.txt"
lcSendFileName="d:\redp\output\customer\website\test1.txt"
lnSec=seconds()
inFile= fopen(lcFileName) && Open a file
if inFile<0 && File could not be opened
     wait window nowait "File "+lcFileName+" could not be opened!"
     return .f.
endif
outFile=fcreate(lcSendFileName) && Create a file
if outFile<0 && File could not be created
     wait window nowait "File "+lcSendFileName+" could not be created!"
     return .f.
endif
*lii=0
do while !feof(inFile)
     lcBuffer=fread(inFile,10000) && Get the string
     lcBuffer= strtran (lcBuffer,["],'') && Remove double quotes
     =fwrite(outFile,lcBuffer)
enddo
=fclose(inFile)        && Close In file
=fclose(outFile)       && Close Out file
?seconds()-lnSec
*modi file (lcSendFileName)
lcSendFileName="d:\redp\output\customer\website\test2.txt"
lnSec=seconds()
inFile= fopen(lcFileName) && Open a file
if inFile<0 && File could not be opened
     wait window nowait "File "+lcFileName+" could not be opened!"
     return .f.
endif
outFile=fcreate(lcSendFileName) && Create a file
if outFile<0 && File could not be created
     wait window nowait "File "+lcSendFileName+" could not be created!"
     return .f.
endif
lii=0
do while !feof(inFile)
     lcBuffer=fgets(inFile,1000) && Get the string
     lcBuffer= strtran (lcBuffer,["],'') && Remove double quotes
     =fputs(outFile,lcBuffer)
*!*          lii=lii+1
*!*          =fseek(inFile,0,lii*8000)
*!*          =fseek(outFile,0,lii*8000)
enddo
=fclose(inFile)        && Close In file
=fclose(outFile)       && Close Out file
?seconds()-lnSec
If files are sitting on the local drive, the second approach works faster (difference about 1 sec.). If they are located on the NetWork drive, the first approach is faster ~3sec. File is 5MB.

I also can not find the optimum size in fread().

>>Looks like with FREAD() I have to re-position with FSEEK() after each read. Do you think it still would be faster? BTW, VFP Help on FREAD() is really scare. I checked Hacker's Guide, it doesn't show the maximum number of bytes for FREAD().
>
>Reposition? Why? It will leave you exactly at the first unread byte. If you're writing a single pass parser, that's all you need. If you want to write stuff back into the same file, that'd be a different story - and keep in mind that, after removing the quotation marks, your resulting file would be shorter anyway. I wrote this example assuming you'll be creating a second file (even if it means killing the first one in the end). Writing it back into the same file would require to keep track of two locations: the beginning of the previous block, and the beginning of the next block, i.e. the write and the read position. You can obtain both using fseek() after each write or read.
>
>I guess maximal number of bytes for FRead() was increased in 5 or 6; initially it was something like 8K or so. I've seen examples where FRead() was used instead of FileToStr() before the latter was introduced, which could mean you have the same limitation as for any other string (16M?).
If it's not broken, fix it until it is.


My Blog
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform