'Example api code snippets to open a file for input, output, 'and append with error trapping.'Also api code for 'line input', 'print #file', and getting the 'whole file at once.'Numeric data must be converted to string before writing it'to a file.'chr$(13) + chr$(10) is a carriage return + linefeed'LB strips the carriage return/linefeed with "line input #file"'and adds it with "print #file". That behavior is also used here.'LB does not add a crlf if the print statement is followed by'a semicolon. That behavior is not used here, although it'can be duplicated with slightly different code. In fact, all'LB file operations can be done using api calls. Most, if not all, of'the api calls needed are used here. Having said that, the main reason'for using api file operations instead of LB's is for the error'trapping and recovery that can be performed. Maybe LB4 will include 'error trapping and make api file operations unnecessary.'*****************************************************************' snippets'*****************************************************************
eoFile(0)=0'global "end of file" indicator
hFile =0'file handle
INVALID.HANDLE.VALUE =-1'Do not use hexdec("&Hffffffff") or _INVALID_HANDLE_VALUE'******************* Open a file for input ********************filedialog"Open text file","*.txt", fileName$
if fileName$ =""thenend'open a file for input
hFile = OpenForInput(fileName$)if hFile <> INVALID.HANDLE.VALUE then'file opened successfully'Get file contents one line at a time.while eoFile(0)=0
content$ = LineInput$(hFile)print content$
wendnotice"DONE"print'OR: Get the file contents all at once.' content$ = GetFileContent$(hFile)' print content$elsenotice"File Access Error"+chr$(13)+"File could not be opened."endifcall CloseFile hFile
wait'******************* Open a file for append ********************filedialog"Open a file for append","*.txt", fileName$
if fileName$ =""thenend'open a file for append
hFile = OpenForAppend(fileName$)if hFile <> INVALID.HANDLE.VALUE then'file opened successfully'Append some text to the file.
r = writeFile(hFile,"First appended line")
r = writeFile(hFile,"Second appended line")
r = writeFile(hFile,"Third appended line")'For additional error checking, r could be tested for 0.'0 would indicate that the data was not written to the file.elsenotice"File Access Error"+chr$(13)+"File could not be opened."endifcall CloseFile hFile
wait'******************* Open a file for output ********************filedialog"Open a file for output","*.txt", fileName$
if fileName$ =""thenend'open a file for output
hFile = OpenForOutput(fileName$)if hFile <> INVALID.HANDLE.VALUE then'file opened successfully'Print some text to the file.
r = writeFile(hFile,"First line of text")
r = writeFile(hFile,"Second line of text")
r = writeFile(hFile,"Third line of text")'For additional error checking, r could be tested for 0.'0 would indicate that the data was not written to the file.elsenotice"File Access Error"+chr$(13)+"File could not be opened."endifcall CloseFile hFile
wait'*****************************************************************' api file handling: subs / functions '*****************************************************************function OpenForInput(fileName$)'open a file for input.calldll#kernel32,"CreateFileA", fileName$ asptr, _GENERIC_READ asulong, _
0asulong,0aslong, _OPEN_EXISTING asulong, _
_FILE_ATTRIBUTE_NORMAL asulong,0aslong, OpenForInput aslongendfunctionfunction OpenForOutput(fileName$)'Open a file for output. 'If file doesn't exist it will be created.'If it does exist, it will be opened and the contents erased.calldll#kernel32,"CreateFileA", fileName$ asptr, _GENERIC_WRITE asulong, _
0asulong,0aslong, _CREATE_ALWAYS asulong, _
_FILE_ATTRIBUTE_NORMAL asulong,0aslong, OpenForOutput aslongendfunctionfunction OpenForAppend(fileName$)'Open a file for append. 'If the file does not exist it will be created.'If it does exist, it will be opened and the file'pointer set to the end of the file.calldll#kernel32,"CreateFileA", fileName$ asptr, _GENERIC_WRITE asulong, _
0asulong,0aslong, _OPEN_ALWAYS asulong, _
_FILE_ATTRIBUTE_NORMAL asulong,0aslong, hFile aslong'set the file pointer to the end of the filecalldll#kernel32,"SetFilePointer", hFile aslong,0aslong,0aslong, _
_FILE_END asulong, r asulong
OpenForAppend = hFile
endfunctionsub CloseFile hFile
calldll#kernel32,"CloseHandle", hFile aslong, r asbooleanendsubfunction LineInput$(hFile)'get 1 line from a file opened with OpenForInput'without the carriage return/linefeed.struct BytesRead, pcb asulong
eoFile(0)=0'reset "end of file" indicator'i(0) is used as a static file pointer variablewhile x =0'depend on exit while to leave loop
pbBuff$ =space$(512)calldll#kernel32,"ReadFile", hFile aslong, pbBuff$ asptr, _
512aslong, BytesRead asstruct,0aslong, r aslongif BytesRead.pcb.struct>0then
i =instr(pbBuff$,chr$(13))if i >0then
LineInput$ = LineInput$ +left$(pbBuff$,i-1)
fptr = i +1+ i(0)
i(0)= fptr
calldll#kernel32,"SetFilePointer", hFile aslong, fptr aslong,0aslong, _
_FILE_BEGIN asulong, r asulongexitwhileelse
LineInput$ = LineInput$ + pbBuff$
i(0)= i(0)+len(pbBuff$)endifelse
eoFile(0)=-1
i(0)=0exitwhileendifwendendfunctionfunction GetFileContent$(hFile)'get the entire contents of a file opened with OpenForInputstruct BytesRead, pcb asulong'set the file pointer to the beginning of the filecalldll#kernel32,"SetFilePointer", hFile aslong,0aslong,0aslong, _
_FILE_BEGIN asulong, r asulong'Get the file size. As used here, the max file size this will accurately return 'is 4,294,967,295 bytes.calldll#kernel32,"GetFileSize", hFile aslong,0asulong, size asulong'read the whole file into pbBuff$if size >0then
pbBuff$ =space$(size)calldll#kernel32,"ReadFile", hFile aslong, pbBuff$ asptr, _
size aslong, BytesRead asstruct,0aslong, r aslong
GetFileContent$ = pbBuff$
pbBuff$ =""endifendfunctionfunction writeFile(hFile,data$)'print a line of text to a file opened with OpenForOutput or OpenForAppend
d$ =data$ +chr$(13)+chr$(10)struct BytesWritten, pcb asulong
BytesToWrite =len(d$)calldll#kernel32,"WriteFile", hFile aslong, d$ asptr, _
BytesToWrite aslong, BytesWritten asstruct,0aslong, r aslong
writeFile = BytesWritten.pcb.structendfunction
API-Based File Operations
Commented Demo-