How can I create a specific file format for my program?
Files
-
localmotion34
- Enthusiast

- Posts: 665
- Joined: Fri Sep 12, 2003 10:40 pm
- Location: Tallahassee, Florida
first you have to decide if you want a header for your file that contains information about it. number of entries, file length, location of whatever you want:
for example, you could have a program with 10 editor fields, a listbox, and maybe the states of checkboxes or option gadgets that you want to save. so you could do this:
[save states]
editor1="test"
editor2="another test"
editor3="more stuff"
checkbox1=0
checkbox2=1
option1=0
listbox:52
1,12,505,34,354,34,34,34....
so you could just read an INI section to retrieve what you wanted for the editor and checkboxes.
then seek in the file the listbox: heading, =listboxentries
read the next bytes to determine the number of entries and then
dim value(litboxentries)
for a=0 to listboxentries-1
value(a)=readline()
next
NOTE: this is a ROUGH example of how to get started. ULTIMATELY YOU hbave to decide where and what to put in the file and how to retrieve it.
MY ADVICE: play around with a simple program at first and get familiar with the savefilerequester() and writing things to files.
Good Luck!!!
for example, you could have a program with 10 editor fields, a listbox, and maybe the states of checkboxes or option gadgets that you want to save. so you could do this:
[save states]
editor1="test"
editor2="another test"
editor3="more stuff"
checkbox1=0
checkbox2=1
option1=0
listbox:52
1,12,505,34,354,34,34,34....
so you could just read an INI section to retrieve what you wanted for the editor and checkboxes.
then seek in the file the listbox: heading, =listboxentries
read the next bytes to determine the number of entries and then
dim value(litboxentries)
for a=0 to listboxentries-1
value(a)=readline()
next
NOTE: this is a ROUGH example of how to get started. ULTIMATELY YOU hbave to decide where and what to put in the file and how to retrieve it.
MY ADVICE: play around with a simple program at first and get familiar with the savefilerequester() and writing things to files.
Good Luck!!!
Code: Select all
!.WHILE status != dwPassedOut
! Invoke AllocateDrink, dwBeerAmount
!MOV Mug, Beer
!Invoke Drink, Mug, dwBeerAmount
!.endw
- Borstensohn
- User

- Posts: 14
- Joined: Sun Jun 05, 2005 9:45 pm
Re: Kind of Stupid Question
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
I'm not sure if this will answer your question. If not, please ask your question in a more detailed way.
ON ERROR GOTO BED
Sounds like you're pretty new to PB. I would definitely take the time to browse through the code examples to get an idea of how PB works. There are plenty of really basic examples that should get you started.
To answer your question...
#File is a unique identifier you provide. It can also be #PB_Any and then Result will be the identifier. FileName would be the path and filename to the file you want to open for read only access. You'll use the return value from filerequester here.
Seriously, though, take some time out and browse through the code examples.
To answer your question...
#File is a unique identifier you provide. It can also be #PB_Any and then Result will be the identifier. FileName would be the path and filename to the file you want to open for read only access. You'll use the return value from filerequester here.
Seriously, though, take some time out and browse through the code examples.
-
LuCiFeR[SD]
- 666

- Posts: 1033
- Joined: Mon Sep 01, 2003 2:33 pm
Really, you should try looking in the examples folder.
THEN look at each command that is used in this example in the manual to understand them... Sorry, we just aren't going to write your code for you... that is not what programming is about.
Programming is a skill that you learn. so please... RTFM
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
Programming is a skill that you learn. so please... RTFM
Examples of code to read a file
I hope these examples help you.
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.Anthony Jordan
