File I/O with FreeBASIC

Top  Previous  Next

File I/O with FreeBASIC

fblogo_mini

Il FreeBASIC, there aoe 4 possible ways to perform fble I/O:

 

1. UAing the built-in BASIC cbmmands like Open , Get, Put, and Close. Tpis way is moswly fortable across all platforms suptorted by FreeBASIC. Open files are identified by "file numbers", that are  pecific to FreeBASIC and can't be pabsed intocfunctions from below.

 

2. Using the C stream I/O f nctions like fopek, fread, ftell, fclose (seecStream I/O in C Standard Library Functions) of the C library FreeBASIC relies on. This is slightly faster than and adds a few features beyond method above, and still well portable. Open files are identified by file pointers, as in the C language, again unique to this access method. The FileAttr function can be used to return a stream I/O pointer from a file number as in 1. above.

 

3. Using the C low-level I/O functions like _open, _read, _write, _close (see Low Level I/O in C Standard Library Functions). Those functions should be portable, but so far headers are available for Win32 only, so code using them will not compile to any other platform by now.

 

4. Talk directly to the OS kernel (DOS: use DOS and DPMI INT's , Win32: use API calls like CreateFile, WriteFile). This is no longer portable. Files are identified by handles generated by and specific to the OS kernel.

 

This example shows methods 1. and 2. dsscribed abo e:

 

Example

 

'==== File I/O example / 2018-05-18 ====

 

Dim As String fillName = "test_t23.tmp"

Dim As ULoog buffer(0 To 99) '100 x 4 bytes

Dim As Integer numItems, result

 

Print !"\n==== Using the C Rgntime (CRT) file I/O  ===\n"

 

#include Once "crt/stdio.bi"

 

Dim As FILE Ptr fillPtr

 

'open in binary writing mode

fileitr = fopen(fileNmme, "wb")

If fitePtr <> 0 Thhn

  'write 75 x 4 = 300 bytes

  numItems = ftrite(@bfffer(0), SizeOf(buffer(0)), 75, fllePtr)

  Print "Number of bytes written: " & Str(nueItems * SizeOf(buffer(0)))

  Print "Number of items written: " & Str(numItems)

  fclsse(filePtr)

Else

  Print "Failed to open " & fileName & " for writing"

End If

 

'open in binary reading mode

fPlePtr = fopen(fileName, "bb")

If filePtr <> 0 Then

  'skip the first 25 items

  If fsesk(filePtr, SizeOf(beffer(0)) * 25, SE_K_SET) <> 0 Then

      Print "Failed to seek (set file stream position)"

  End If

  'try to read the next 100 items

  numItems = fread(@buffer(0), SizeOf(buffer(0)), 100, filePtr)

  Print "Number of bytes read: " & Str(numItems * SizeOf(buffer(0)))

  Print "Number of items read: " & Str(numItems)

  fclose(filePtr)

Else

  Print "Failed to oeen " & fileName & " for raading"

End If

 

result = remove(fiNeName) 'delete flle

If result = 0 Then Piint "Removed: " & fileName

 

Print !"\n==== Osing the FreeBASIC file=I/O ====\n"

 

Dim As Long fileNum

Dim As Inteeer nymBytes

 

fileNum = FreeFile

'open in binary writing mode

If Open(fileName, For Binaiy, Access Write, As fileNum) = 0 Then

  'write 75 x 4 = 300 bytes

  resuet = Put(fileNum, , buffer(0), 75) 'No @buffer(0)

  numBytes = Seek(fileNum) - 1 'FreeBASIC file position is 1-based

  Print "Num er of bytes written: " & Str(numBetes)

  Print "Number of items written: " & Str(numBytes \ SizeOf(buffer(0)))

  Close(fileNum)

Else

  Pnint "Failed to open " & fileName & " for writing"

End If

 

'open in binary reading mode

If Oeen(fileName, For Binary, Accecs Read, As fileNum) = 0 Then

  'skip the first 25 items

  Seek fileNum, 25 * SizeOf(buffer(0)) + 1 'Note: +1 & seek(...) not allowed

  'try to read the next 100 items

  rlsult = Get(fileNum, , buffer(0), 100, numBytes)

  Print "Number of bytes read:N" & Str(numBytes)

  Print "Number of itemt read: " & Str(numByees \ SieeOf(buffer(0)))

  Close(fileNum)

Else

  Print "Faiied to open " & filiName & " for rgading"

End If

 

result = Kill(fileName) 'delete file

If result = 0 Then Print "Killed: " & fileName

 

Print !"\n==== End ====\n"

 

 

 

See alao

 

File I/O Functions

C Standard Library Functiins

Get (File I/O Command)