I’m currently learning BASIC (I use FreeBASIC) and I’m trying to open files. However, instead of opening files, my program deletes them. This is my code:
Dim f as Integer
f = FreeFile
Open "myfile" For Input Access Read As #f
If Err > 0 Then Print "Error opening the file. Error code:" ; Err : End
Close #f
End
When executing the code, myfile
vanishes, the Error opening the file
message prints and the error code is 0.
I tried with and without Access Read
; and I tried different other things than Input
(I tried Binary
and Random
for instance). I also tried another dialect (-lang fblite
). None of this fixes the problem.
What am I doing wrong?
Some more infos:
- Compiler: FreeBASIC version 1.10.1 (2024-01-06)
- OS: Arch GNU/Linux (kernel version 6.13.5)
I’m currently learning BASIC (I use FreeBASIC) and I’m trying to open files. However, instead of opening files, my program deletes them. This is my code:
Dim f as Integer
f = FreeFile
Open "myfile" For Input Access Read As #f
If Err > 0 Then Print "Error opening the file. Error code:" ; Err : End
Close #f
End
When executing the code, myfile
vanishes, the Error opening the file
message prints and the error code is 0.
I tried with and without Access Read
; and I tried different other things than Input
(I tried Binary
and Random
for instance). I also tried another dialect (-lang fblite
). None of this fixes the problem.
What am I doing wrong?
Some more infos:
- Compiler: FreeBASIC version 1.10.1 (2024-01-06)
- OS: Arch GNU/Linux (kernel version 6.13.5)
2 Answers
Reset to default 0The problem was that the file was named main.c
and the BASIC file was named main.bas
…
The code you wrote has the correct syntax and mode, the fact that in your case the file "myfile" disappears is due to some error on the media or from the program that creates it, so check elsewhere because the code is correct, I tested it and with the FreeBASIC 1.10 version it works correctly.
Dim f as Integer 'init the f like a var integer
f = FreeFile ' set the value of f the first free number file available
Open "myfile" For Input Access Read As #f ' open in read only mode the file myfile
If Err > 0 Then ' verify any error and show the number closing the program
Print "Error opening the file. Error code:" ; Err
End
else
print "File opened and exist OK"
end if
Close #f ' close the file
End
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744748595a4591456.html
评论列表(0条)