
How can I create a specific file format for my program?
Code: Select all
!.WHILE status != dwPassedOut
! Invoke AllocateDrink, dwBeerAmount
!MOV Mug, Beer
!Invoke Drink, Mug, dwBeerAmount
!.endw
You'll find the answer in PureBasic's online help. Look for topic "ReadFile". This opens a file for reading only. If you want to open a file for reading and writing, use "OpenFile".sk8terboi wrote:If I want to open a file. What code do I use to have the file open
Code: Select all
;
; ------------------------------------------------------------
;
; PureBasic - File example file
;
; (c) 2001 - Fantaisie Software
;
; ------------------------------------------------------------
;
MessageRequester("PureBasic", "Welcome !"+Chr(13)+"PureBasic will write a file named: PureBasicTestFile.pb", 0)
If CreateFile(0, "PureBasicTestFile.txt")
WriteStringN(" This is a PureBasic file test")
WriteString("Now it's on ")
WriteString("the same line.")
CloseFile(0)
Else
MessageRequester("PureBasic", "Error: can't write the file", 0)
End
EndIf
If ReadFile(0, "PureBasicTestFile.txt")
First$ = Trim(ReadString())
MessageRequester("PureBasic", "Line read: "+First$, 0)
CloseFile(0)
Else
MessageRequester("PureBasic", "Error: Can't read the file", 0)
EndIf
End
Code: Select all
; Examples of reading a file
; If you wish to see the file contents, debugging must be enabled
path$ = "C:\Myfile.txt" ; *** Replace by the pathname of an existing text file
; Example 1 (normal method)
res = ReadFile(1, path$)
If res=0
MessageRequester("Error","Could not open input file")
End
EndIf
Repeat
ln$ = ReadString()
Debug ln$
Until Eof(1)
CloseFile(1)
Debug "-------"
; Example 2 (variant method)
If ReadFile(2, path$)=0
MessageRequester("Error","Could not open input file"): End
EndIf
While Loc()<Lof(): Debug ReadString(): Wend
CloseFile(2)
Debug "-------"
; Example 3 (using a file requester)
pattern$ = "Text file (*.txt)|*.txt|Any file|*.*"
path$ = OpenFileRequester("Select a text file", "", pattern$, 0)
If Len(path$)=0
MessageRequester("Abort","You have cancelled the filename request")
End
EndIf
; The remaining code is identical to that in Example 1 or 2.