Files

Just starting out? Need help? Post your questions and find answers here.
sk8terboi
User
User
Posts: 26
Joined: Mon Jun 06, 2005 3:40 am

Files

Post by sk8terboi »

:D
How can I create a specific file format for my program?
Josh
localmotion34
Enthusiast
Enthusiast
Posts: 665
Joined: Fri Sep 12, 2003 10:40 pm
Location: Tallahassee, Florida

Post by localmotion34 »

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!!!

Code: Select all

!.WHILE status != dwPassedOut
! Invoke AllocateDrink, dwBeerAmount
!MOV Mug, Beer
!Invoke Drink, Mug, dwBeerAmount
!.endw
sk8terboi
User
User
Posts: 26
Joined: Mon Jun 06, 2005 3:40 am

Kind of Stupid Question

Post by sk8terboi »

If I want to open a file. What code do I use to have the file open :?:
Josh
User avatar
Borstensohn
User
User
Posts: 13
Joined: Sun Jun 05, 2005 9:45 pm

Re: Kind of Stupid Question

Post by Borstensohn »

sk8terboi wrote:If I want to open a file. What code do I use to have the file open :?:
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".

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
akee
Enthusiast
Enthusiast
Posts: 496
Joined: Wed Aug 18, 2004 9:52 am
Location: Penang, Malaysia

Post by akee »

sk8terboi wrote:If I want to open a file. What code do I use to have the file open :?:
Or you can use the Win32 API function CreateFile_()... :)
sk8terboi
User
User
Posts: 26
Joined: Mon Jun 06, 2005 3:40 am

Reply

Post by sk8terboi »

So it gave me a formula. Result = ReadFile(#File, FileName$)
If I am having a filerequester, what do I put for #file, FileName$ :?: :?:
Josh
Xombie
Addict
Addict
Posts: 898
Joined: Thu Jul 01, 2004 2:51 am
Location: Tacoma, WA
Contact:

Post by Xombie »

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.
sk8terboi
User
User
Posts: 26
Joined: Mon Jun 06, 2005 3:40 am

Sorry

Post by sk8terboi »

Sorry, I am new to PB. I have studied many of the examples and still have 1 question. Can you give me an example of the code for
Result = ReadFile(#File, FileName$) :?: :?: :?: :?: :?: :?: :?: :?: :?: :?: :?: :?: :?:

Thanks for your time :!: :!: :!:
Josh
LuCiFeR[SD]
666
666
Posts: 1033
Joined: Mon Sep 01, 2003 2:33 pm

Post by LuCiFeR[SD] »

Really, you should try looking in the examples folder.

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 
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 :twisted:
akj
Enthusiast
Enthusiast
Posts: 668
Joined: Mon Jun 09, 2003 10:08 pm
Location: Nottingham

Examples of code to read a file

Post by akj »

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
Post Reply