Page 1 of 1

Files

Posted: Mon Jun 06, 2005 3:43 am
by sk8terboi
:D
How can I create a specific file format for my program?

Posted: Mon Jun 06, 2005 3:53 am
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!!!

Kind of Stupid Question

Posted: Mon Jun 06, 2005 3:58 am
by sk8terboi
If I want to open a file. What code do I use to have the file open :?:

Re: Kind of Stupid Question

Posted: Mon Jun 06, 2005 1:33 pm
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.

Posted: Mon Jun 06, 2005 2:04 pm
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_()... :)

Reply

Posted: Mon Jun 06, 2005 7:26 pm
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$ :?: :?:

Posted: Mon Jun 06, 2005 8:04 pm
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.

Sorry

Posted: Mon Jun 06, 2005 8:19 pm
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 :!: :!: :!:

Posted: Mon Jun 06, 2005 10:59 pm
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:

Examples of code to read a file

Posted: Mon Jun 06, 2005 11:01 pm
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.