Today I found interesting thing. I had to work with text files, that
have to be importd into the tables. Before they can be imported, a
couple operations have to be done.These files are downloaded from
mainframe.
First one was "joining" two lines into one so it suits the structure of
the table. ( limitation of our ES 9000 is 254 bytes and some )
The second one of them was removing of "control line" (it says how many
records the file contains, so after append it can be checked and
verified) at the bottom of text file, so it is not appended to the
table. In fact I didn't want to append 65-100K records, delete the last
one and pack it, I chose creating a new text file without the last line.
What I did was that I opened the text file using the low-level functions
for reading and created new temporary file. Then I just read the first
one through and write down in the new one, until I get tho that "control
line".(I found, that trying to fseek to the desired position in file and
writing it at once failed.).
a sample code:
m.tempfile = 'c:\temp\tempor.txt'
if file (m.tempfile)
dele file (m.tempfile)
endif
handle1 = fopen ( m.sourcefile, 0 )
handle2 = fcreate ( m.tempfile, 0 )
if handle1 < 0 or handle2 < 0
wait wind 'Error creating the file'
= fclose ( handle1 )
= fclose ( handle2 )
return .f.
else
m.go_on = .t.
do while m.go_on
m.lineoftext = fgets (handle1)
if left ( m.lineoftext, 11 ) = '##########:'
m.go_on = .f.
else
= fputs (handle2, m.lineoftext)
endif
enddo
endif
= fclose ( handle1 )
= fclose ( handle2 )
So, it looks good. And it's supposed to work. BUT it doesn't. After two
hours searching why I found out what was problem. Here's the correctly
working code.
m.tempfile = 'c:\temp\tempor.txt'
if file (m.tempfile)
dele file (m.tempfile)
endif
handle1 = fopen ( m.sourcefile, 0 )
handle2 = fcreate ( m.tempfile, 0 )
if handle1 < 0 or handle2 < 0
wait wind 'Error creating the file'
= fclose ( handle1 )
= fclose ( handle2 )
return .f.
else
m.go_on = .t.
do while m.go_on
m.lineoftext = fgets (handle1,512)
if left ( m.lineoftext, 11 ) = '##########:'
m.go_on = .f.
else
= fputs (handle2, m.lineoftext)
endif
enddo
endif
= fclose ( handle1 )
= fclose ( handle2 )
As help says, fgets command should read sequence of bytes until it
encounters CRLF, it doesn't and stops at 254-byte and divides one line
into two!!!! It known or not.
Comments, appreciated.
Dezider
--
There were three worst disasters in the history of human
race in 20-th century:
1, Hiroshima 44
2, Tchernobyl 86
3, Windows 95