|
6 | 6 | '' See Also: https://www.freebasic.net/wiki/wikka.php?wakka=ProPgFileIO |
7 | 7 | '' -------- |
8 | 8 |
|
9 | | -Data " File I/O example & test GET vs FREAD | (CL) 2008-10-12 Public Domain " |
10 | | -Data " http://www.freebasic.net/wiki/wikka.php?wakka=ProPgFileIO " |
11 | | -Rem |
12 | | -Rem Compile With FB 0.20 Or newer |
13 | | -Rem |
14 | | -Rem In the commandline supply preferably 2 different files of same big size |
15 | | -Rem Default Is "BLAH" For both (bad) |
16 | | -Rem In both loops (Get And FREAD) the last Read can be "empty" ... no problem |
| 9 | +'==== File I/O example / 2018-05-18 ==== |
17 | 10 |
|
18 | | -#include "crt\stdio.bi" '' Otherwise the "C"-stuff won't work |
| 11 | +Dim As String fileName = "test_123.tmp" |
| 12 | +Dim As ULong buffer(0 To 99) '100 x 4 bytes |
| 13 | +Dim As Integer numItems, result |
19 | 14 |
|
20 | | -Dim As FILE Ptr QQ '' This is the C-like file access pointer |
21 | | -Dim As UByte Ptr BUF '' Buffer used for both FB-like and C-like read |
22 | | -Dim As UInteger FILN '' FB-like "filenumber" |
| 15 | +Print !"\n==== Using the C Runtime (CRT) file I/O ====\n" |
23 | 16 |
|
24 | | -Dim As UInteger AA, BB, CC, DD, EE |
25 | | -Dim As ULongInt II64 '' We do try to support files >= 4 GiB |
| 17 | +#include Once "crt/stdio.bi" |
26 | 18 |
|
27 | | -Dim As String VGSTEMP, VGSFILE1, VGSFILE2 |
| 19 | +Dim As FILE Ptr filePtr |
28 | 20 |
|
29 | | -? : Read VGSTEMP : ? VGSTEMP : Read VGSTEMP : ? VGSTEMP : ? |
| 21 | +'open in binary writing mode |
| 22 | +filePtr = fopen(fileName, "wb") |
| 23 | +If filePtr <> 0 Then |
| 24 | + 'write 75 x 4 = 300 bytes |
| 25 | + numItems = fwrite(@buffer(0), SizeOf(buffer(0)), 75, filePtr) |
| 26 | + Print "Number of bytes written: " & Str(numItems * SizeOf(buffer(0))) |
| 27 | + Print "Number of items written: " & Str(numItems) |
| 28 | + fclose(filePtr) |
| 29 | +Else |
| 30 | + Print "Failed to open " & fileName & " for writing" |
| 31 | +End If |
30 | 32 |
|
31 | | -VGSTEMP=Command$(1) : VGSFILE1="BLAH" |
32 | | -If (VGSTEMP<>"") Then VGSFILE1=VGSTEMP |
33 | | -VGSTEMP=Command$(2) : VGSFILE2=VGSFILE1 |
34 | | -If (VGSTEMP<>"") Then VGSFILE2=VGSTEMP |
| 33 | +'open in binary reading mode |
| 34 | +filePtr = fopen(fileName, "rb") |
| 35 | +If filePtr <> 0 Then |
| 36 | + 'skip the first 25 items |
| 37 | + If fseek(filePtr, SizeOf(buffer(0)) * 25, SEEK_SET) <> 0 Then |
| 38 | + Print "Failed to seek (set file stream position)" |
| 39 | + End If |
| 40 | + 'try to read the next 100 items |
| 41 | + numItems = fread(@buffer(0), SizeOf(buffer(0)), 100, filePtr) |
| 42 | + Print "Number of bytes read: " & Str(numItems * SizeOf(buffer(0))) |
| 43 | + Print "Number of items read: " & Str(numItems) |
| 44 | + fclose(filePtr) |
| 45 | +Else |
| 46 | + Print "Failed to open " & fileName & " for reading" |
| 47 | +End If |
35 | 48 |
|
36 | | -BUF = Allocate(32768) '' 32 KiB - hoping it won't fail, BUF could be 0 ... |
| 49 | +result = remove(fileName) 'delete file |
| 50 | +If result = 0 Then Print "Removed: " & fileName |
37 | 51 |
|
38 | | -? : ? "FB - OPEN - GET , """+VGSFILE1+"""": Sleep 1000 |
39 | | -FILN = FreeFile : AA=0 : II64=0 '' AA counts blocks per 32 KiB already read |
40 | | -BB=Open (VGSFILE1 For Binary Access Read As #FILN) |
41 | | -'' Result 0 is OK here, <>0 is evil |
42 | | -'' "ACCESS READ" should prevent file creation if it doesn't exist |
43 | | -? "OPEN result : " ; BB |
44 | | -If (BB=0) Then '' BB will be "reused" for timer below |
45 | | - BB=Cast(UInteger,(Timer*100)) '' No UINTEGER TIMER in FB, make units 10 ms |
46 | | - CC=Get (#FILN,,*BUF,32768,DD) |
47 | | - '' CC has the success status, 0 is OK, <>0 is bad |
48 | | - '' DD is the amount of data read |
49 | | - '' EOF is __NOT__ considered as error here |
50 | | - ? "0th GET : ";CC;" ";DD |
51 | | - ? "2 bytes read : ";BUF[0];" ";BUF[1] |
52 | | - Do |
53 | | - AA=AA+1 : II64=II64+Cast(ULongInt,DD) |
54 | | - If (DD<32768) Or (CC<>0) Then Exit Do '' Give up |
55 | | - CC=Get (#FILN,,*BUF,32768,DD) |
56 | | - Loop |
57 | | - EE=Cast(UInteger,(Timer*100))-BB |
58 | | - ? "Time : ";(EE+1)*10;" ms" |
59 | | - If (AA>1) Then ? "Last GET : ";CC;" ";DD |
60 | | - ? "Got __EXACTLY__ ";II64;" bytes in ";AA;" calls" |
61 | | - Close #FILN |
62 | | -ENDIF |
| 52 | +Print !"\n==== Using the FreeBASIC file I/O ====\n" |
63 | 53 |
|
64 | | -? : ? "C - FOPEN - FREAD , """+VGSFILE2+"""" : Sleep 1000 |
65 | | -AA=0 : II64=0 '' AA counts blocks per 32 KiB already read |
66 | | -QQ=FOPEN(VGSFILE2,"rb") |
67 | | -'' Here 0 is evil and <>0 good, opposite from above !!! |
68 | | -'' File will not be created if it doesn't exist (good) |
69 | | -'' "rb" is case sensitive and must be lowercase, STRPTR seems not necessary |
70 | | -? "FOPEN result : " ; QQ |
71 | | -If (QQ<>0) Then |
72 | | - BB=Cast(UInteger,(Timer*100)) '' No UINTEGER TIMER in FB, make units 10 ms |
73 | | - DD=FREAD(BUF,1,32768,QQ) '' 1 is size of byte - can't live without :-D |
74 | | - '' Returns size of data read, <32768 on EOF, 0 after EOF, or "-1" on error |
75 | | - ? "0th FREAD : ";DD |
76 | | - ? "2 bytes read : ";BUF[0];" ";BUF[1] |
77 | | - Do |
78 | | - AA=AA+1 |
79 | | - If (DD<=32768) Then II64=II64+Cast(ULongInt,DD) |
80 | | - If (DD<>32768) Then Exit Do '' ERR or EOF |
81 | | - DD=FREAD(BUF,1,32768,QQ) |
82 | | - Loop |
83 | | - EE=Cast(UInteger,(Timer*100))-BB |
84 | | - ? "Time : ";(EE+1)*10;" ms" |
85 | | - If (AA>1) Then ? "Last FREAD : ";DD |
86 | | - ? "Got __EXACTLY__ ";II64;" bytes in ";AA;" calls" |
87 | | - FCLOSE(QQ) |
88 | | -ENDIF |
| 54 | +Dim As Long fileNum |
| 55 | +Dim As Integer numBytes |
89 | 56 |
|
90 | | -Deallocate(BUF): Sleep 1000 '' Crucial |
| 57 | +fileNum = FreeFile |
| 58 | +'open in binary writing mode |
| 59 | +If Open(fileName, For Binary, Access Write, As fileNum) = 0 Then |
| 60 | + 'write 75 x 4 = 300 bytes |
| 61 | + result = Put(fileNum, , buffer(0), 75) 'No @buffer(0) |
| 62 | + numBytes = Seek(fileNum) - 1 'FreeBASIC file position is 1-based |
| 63 | + Print "Number of bytes written: " & Str(numBytes) |
| 64 | + Print "Number of items written: " & Str(numBytes \ SizeOf(buffer(0))) |
| 65 | + Close(fileNum) |
| 66 | +Else |
| 67 | + Print "Failed to open " & fileName & " for writing" |
| 68 | +End If |
91 | 69 |
|
92 | | -End |
| 70 | +'open in binary reading mode |
| 71 | +If Open(fileName, For Binary, Access Read, As fileNum) = 0 Then |
| 72 | + 'skip the first 25 items |
| 73 | + Seek fileNum, 25 * SizeOf(buffer(0)) + 1 'Note: +1 & seek(...) not allowed |
| 74 | + 'try to read the next 100 items |
| 75 | + result = Get(fileNum, , buffer(0), 100, numBytes) |
| 76 | + Print "Number of bytes read: " & Str(numBytes) |
| 77 | + Print "Number of items read: " & Str(numBytes \ SizeOf(buffer(0))) |
| 78 | + Close(fileNum) |
| 79 | +Else |
| 80 | + Print "Failed to open " & fileName & " for reading" |
| 81 | +End If |
| 82 | + |
| 83 | +result = Kill(fileName) 'delete file |
| 84 | +If result = 0 Then Print "Killed: " & fileName |
| 85 | + |
| 86 | +Print !"\n==== End ====\n" |
0 commit comments