Direct Acces File !

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
GeBonet
Enthusiast
Enthusiast
Posts: 135
Joined: Fri Apr 04, 2008 6:20 pm
Location: Belgium

Direct Acces File !

Post by GeBonet »

Hi,
Since I know PB, I wonder why there is no direct access to the file?
Such as this, that everyone knows in a lot of language:

Code: Select all

Structure Etudiants
  Student.s[25]
  Annees.s
  Points.i
  Total.l
  Moyenne.f
  Commentaire.s[50]
End TYPE
Dim MaClasse.Etudiants(150)
; 
OpenFile(#Buffer, Nomfichier$ AS Random, Len(SizeOf(MaClasse)))
PUT #Buffer, Record, MaClasse(Record)
  
; --------------------------------------------------------
OpenFile(#Buffer, Nomfichier$ AS Random, Len(SizeOf(MaClasse)))
GET #Buffer, Record, MaClasse(Record)
I know this is possible by other means,
but if it were implemented then it would still be easier and more would lock the record is read / written!
Thank you for reading! :D
Sorry for my english :wink: ! (Windows Xp, Vista and Windows 7, Windows 10)
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Re: Direct Acces File !

Post by gnozal »

ReadData() and WriteData() ...?

Code: Select all

Structure Etudiants
  Student.s[25]
  Annees.s
  Points.i
  Total.l
  Moyenne.f
  Commentaire.s[50]
EndStructure
...
ReadData(File, @MaClasse.Etudiants, SizeOf(Etudiants))
Isn't it easy ?
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
User avatar
GeBonet
Enthusiast
Enthusiast
Posts: 135
Joined: Fri Apr 04, 2008 6:20 pm
Location: Belgium

Re: Direct Acces File !

Post by GeBonet »

Tanks, but it not, the record "n" or "x" of the file... (Like many other language...) Is not one database itself !
It's a random acces file with fixed lenght !
With random-access file, I could use my index sequential access system ...
And put this library on the forum if it works well.
And Alos when i read or write this record is looked !
Tanks for the answers.
PS: Iknow the other and new possibility... And read the entire file in memory....
Sorry for my english :wink: ! (Windows Xp, Vista and Windows 7, Windows 10)
Thorium
Addict
Addict
Posts: 1305
Joined: Sat Aug 15, 2009 6:59 pm

Re: Direct Acces File !

Post by Thorium »

Just use FileSeek to get to your record.
By the way it's not working anyway because you have a variable length string in there, which is nothing else than a memory pointer. You need to take special care of variable length strings.
Last edited by Thorium on Wed Sep 21, 2011 2:35 pm, edited 1 time in total.
User avatar
Rings
Moderator
Moderator
Posts: 1435
Joined: Sat Apr 26, 2003 1:11 am

Re: Direct Acces File !

Post by Rings »

moved topic into 'coding questions', as this can be done
with existing stuff.

PB is not VB ;)
SPAMINATOR NR.1
User avatar
GeBonet
Enthusiast
Enthusiast
Posts: 135
Joined: Fri Apr 04, 2008 6:20 pm
Location: Belgium

Re: Direct Acces File !

Post by GeBonet »

True, PB is not VB, and fortunately, or I do not know what I would do here ... It's also not PowerBasic, not Kbasic, Liberty, Gfabasic, Delphi, Cobol and many other... And in every exist a Random Access File.
But I think there are also other good things and very old good things, like random access (or direct access).
And why not get here because this is PB?

And Random Access File with Fixed Length. Is not Sequential access, with Write/Read.... Is not Binary File...
This mode access File exist also in other language and also in PB (Why ? PB is not other)...
AIso not based on Database and SQL , SQlite etc...

Where in PB, I can ask, give me the fixed lenght record number 33628 and , which contains the data "xx, yy zzz ..." and just after the 12233 ? And when I work on it (the 33628), nobody can read him.
Now, somme thing in French !
Excusez moi... Je crois que je parle mal... ça fait 34 ans que je développe, plus de 200 applications, c'est la première fois que l'on ne comprend pas ce que je dis... L'âge peut-être, que je deviens sénile...
Traductor : Excuse me ... I think I speak bad ... it's been 34 years since I developed, over 200 applications, is the first time that we do not understand what I say ... Age may be, I become senile ...
I thing, I dont understant, the topic is "Feature Requests and Wishlists", it is only a wish
Sorry for the disturbe. :evil:
Sorry for my english :wink: ! (Windows Xp, Vista and Windows 7, Windows 10)
Thorium
Addict
Addict
Posts: 1305
Joined: Sat Aug 15, 2009 6:59 pm

Re: Direct Acces File !

Post by Thorium »

GeBonet wrote:True, PB is not VB, and fortunately, or I do not know what I would do here ... It's also not PowerBasic, not Kbasic, Liberty, Gfabasic, Delphi, Cobol and many other... And in every exist a Random Access File.
But I think there are also other good things and very old good things, like random access (or direct access).
And why not get here because this is PB?

And Random Access File with Fixed Length. Is not Sequential access, with Write/Read.... Is not Binary File...
This mode access File exist also in other language and also in PB (Why ? PB is not other)...
AIso not based on Database and SQL , SQlite etc...

Where in PB, I can ask, give me the fixed lenght record number 33628 and , which contains the data "xx, yy zzz ..." and just after the 12233 ? And when I work on it (the 33628), nobody can read him.
Now, somme thing in French !
Excusez moi... Je crois que je parle mal... ça fait 34 ans que je développe, plus de 200 applications, c'est la première fois que l'on ne comprend pas ce que je dis... L'âge peut-être, que je deviens sénile...
Traductor : Excuse me ... I think I speak bad ... it's been 34 years since I developed, over 200 applications, is the first time that we do not understand what I say ... Age may be, I become senile ...
I thing, I dont understant, the topic is "Feature Requests and Wishlists", it is only a wish
Sorry for the disturbe. :evil:
I am realy confused now. What exactly do you want that is not allready there? If you open a file in PB with OpenFile it is random access. You can read and write structures with ReadData and WriteData, you can jump around in the file with FileSeek.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Direct Acces File !

Post by ts-soft »

For Random Access File you can use the Source from PBOSL found here: http://pbosl.purearea.net/pbosl_src.zip
Use the PBOSL_RFile.pb

The Interface:

Code: Select all

  Interface IRFile
    Create(FileName.s, RecordLength.l)
    Open(FileName.s)
    Close()
    RecCount()
    Eof()
    Lof()
    RecordLength()
    MaxRecord()
    LastUpdate()
    CreationDate()
    Version()
    VersionStr.s()
    Put(RecordNumber.l, MemoryPointer.l)
    Get(RecordNumber.l, MemoryPointer.l)
    Append(MemoryPointer.l)
    Crypt(MemoryPointer.l, PassWord.s)
  EndInterface
A bit old but should work on x86

greetings - Thomas
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
GeBonet
Enthusiast
Enthusiast
Posts: 135
Joined: Fri Apr 04, 2008 6:20 pm
Location: Belgium

Re: Direct Acces File !

Post by GeBonet »

@ts-soft
For Random Access File you can use the Source from PBOSL found here: http://pbosl.purearea.net/pbosl_src.zip
Use the PBOSL_RFile.pb
Thank you, I knew this work and the library PBOSL "_RFile.pb".
What justifies my claim that I had placed in "WishList" BUT "Rings" has moved HERE ...
I think because he thinks like that can be done differently so there is no need ....
For me in this condition we can remove most of the PB function and keep only what you would call the API.s and we will do the same !

If PBSOL found useful to create this lib, it is a useful function. And of course there are ways to do with a library or other that I have also done well.

My claim is precisely that it exists in other native language and it is not useful to create lines and lines of code to function fairly "simple" and common elsewhere. That's it.
Thank you :D
Sorry for my english :wink: ! (Windows Xp, Vista and Windows 7, Windows 10)
infratec
Always Here
Always Here
Posts: 7577
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Direct Acces File !

Post by infratec »

Hi GeBonet,

Maybe this helps you:

Code: Select all

Structure Etudiants
  Student.s[25]
  Annees.s
  Points.i
  Total.l
  Moyenne.f
  Commentaire.s[50]
EndStructure

Dim MaClasse.Etudiants(150)


CompilerIf 0
OpenFile(#Buffer, Nomfichier$ As Random, Len(SizeOf(MaClasse)))
PUT #Buffer, Record, MaClasse(Record)
 
OpenFile(#Buffer, Nomfichier$ As Random, Len(SizeOf(MaClasse)))
GET #Buffer, Record, MaClasse(Record)
CompilerEndIf



Procedure.i GB_Create(Filename.s, FileSize.q)
  
  Result = #False
  
  *Buffer = AllocateMemory(1024)
  If *Buffer
    Result = CreateFile(#PB_Any, Filename)
    If Result
      Repeat
        If FileSize < 1024
          Size = FileSize
        Else
          Size = 1024
        EndIf
        WriteData(Result, *Buffer, Size)
        FileSize - Size
      Until FileSize = 0
    EndIf
    FreeMemory(*Buffer)
  EndIf
  
  ProcedureReturn Result
  
EndProcedure


Procedure.i GB_Open(Filename.s)
  
  Result = OpenFile(#PB_Any, Filename)
  
  ProcedureReturn Result
  
EndProcedure


Procedure GB_Close(File.i)
  
  CloseFile(File)
  
EndProcedure


Procedure.i GB_Put(File.i, RecordNo.l, *Record.Etudiants, RecordSize.i)
  
  Result = #False
  
  RecordNo - 1
  
  FileSeek(File, RecordSize * RecordNo)
  If Loc(File) = RecordSize * RecordNo
    WriteData(File, *Record, RecordSize)
    Result = #True
  EndIf
  
  ProcedureReturn Result
  
EndProcedure


Procedure GB_Get(File.i, RecordNo.l, *Record, RecordSize.i)
  
  Result = #False
  
  RecordNo - 1
  
  FileSeek(File, RecordSize * RecordNo)
  If Loc(File) = RecordSize * RecordNo
    ReadData(File, *Record, RecordSize)
    Result = #True
  EndIf
  
  ProcedureReturn Result
  
EndProcedure





Debug SizeOf(Etudiants)
Debug SizeOf(Etudiants) * 150


Define Test.Etudiants


File = GB_Create("Test.dat", SizeOf(Etudiants) * 150)
If File
  MaClasse(1)\Student = "Test 1"
  MaClasse(1)\Commentaire = "Hello 1"
  GB_Put(File, 1, @MaClasse(1), SizeOf(Etudiants))
  
  MaClasse(2)\Student = "Test 2"
  MaClasse(2)\Commentaire = "Hello 2"
  GB_Put(File, 150, @MaClasse(2), SizeOf(Etudiants))
  
  GB_Get(File, 1, @Test, SizeOf(Etudiants))
  Debug Test\Student
  Debug Test\Commentaire
  
  GB_Get(File, 150, @Test, SizeOf(Etudiants))
  Debug Test\Student
  Debug Test\Commentaire
  
  GB_Close(File)
EndIf

File = GB_Open("Test.dat")
If File
  
  GB_Get(File, 150, @Test, SizeOf(Etudiants))
  Debug Test\Student
  Debug Test\Commentaire
  
  GB_Get(File, 1, @Test, SizeOf(Etudiants))
  Debug Test\Student
  Debug Test\Commentaire
  
  GB_Close(File)
EndIf
I hope that's what you are missing.

Now you have your own (GB_ means GeBonet_) file functions :mrgreen:

Strange is, that I can not find "Test" or "Hello" in the file. :evil:

Bernd
User avatar
Rings
Moderator
Moderator
Posts: 1435
Joined: Sat Apr 26, 2003 1:11 am

Re: Direct Acces File !

Post by Rings »

moved topic back before someone gets a heart attack.....
SPAMINATOR NR.1
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Direct Acces File !

Post by skywalk »

Rings wrote:moved topic back before someone gets a heart attack.....
:lol:
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
GeBonet
Enthusiast
Enthusiast
Posts: 135
Joined: Fri Apr 04, 2008 6:20 pm
Location: Belgium

Re: Direct Acces File !

Post by GeBonet »

Tanks,
Rings wrote:moved topic back before someone gets a heart attack.....
:lol:
With my age and my heart who knows ?? :lol:
But look the wonderfull code from "infratec" above, and i have also one code with direct acces with varialble length record.
All, need some line codes +/- 100-140... Or "ts_Soft" with the PBSOL lib...
With my Wish, I ask only one line ... And two instructions and the code above becomes:

Code: Select all

Structure TestRecord
  Student.s[20]
  Result.f
Endstructure
Dim MyClasse.Etudiants(150)
; ------------------------------------------------------------
; For Write anywhere indicated by "Record"...  Here is the LINE
;
OpenFile(#Buffer, FileName$, RANDOM Len(SizeOf(MaClasse)))
PUTFile (#Buffer, Record, MyClasse(Record))
; -----------------------------------------------------------
; For Read anywhere indicated by "ReadRecord"... 
;
GETfile (#Buffer, ReadRecord, MyClasse(ReadRecord) )
CloseFile(#Buffer)
Is not it easier ? And it exist elsewhere, hence my question and request.
Now if this is not done like so many others, no big deal ... Only.. :cry:
There is "infratec" or many other whith wonderfull solution :lol:
Good night !
Gerhard
Sorry for my english :wink: ! (Windows Xp, Vista and Windows 7, Windows 10)
infratec
Always Here
Always Here
Posts: 7577
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Direct Acces File !

Post by infratec »

Hi,
But look the wonderfull code from "infratec" above, and i have also one code with direct acces with varialble length record.
:oops: :oops: :oops:

But that was the reason that rings moved this topic to Coding Questions.
Because it is easily possible to realize this functions in native PB language.

In the Feature Request and Wishlists area should only appear stuff which is not possible todo easily with native PB commands.

For example:

Select a printer by its name instead of always have to open the PrintRequester().
That is, at the moment, only possible via a lot of API stuff.

Bernd
User avatar
GeBonet
Enthusiast
Enthusiast
Posts: 135
Joined: Fri Apr 04, 2008 6:20 pm
Location: Belgium

Re: Direct Acces File !

Post by GeBonet »

infratec wrote:Hi,
But that was the reason that rings moved this topic to Coding Questions.
Because it is easily possible to realize this functions in native PB language.
In the Feature Request and Wishlists area should only appear stuff which is not possible todo easily with native PB commands.
For example:
Select a printer by its name instead of always have to open the PrintRequester().
That is, at the moment, only possible via a lot of API stuff.
Bernd
I understand what you say... and many others are following simplification or replacement of Subroutine, Function, macros ... I have tons that no longer serve because of being replaced by a day with new features. . So when you can do I can not see why you could not ask? Especially here, an opening line and EGTT or PUT must be 47 lines of code "native" PB ... And excuse me, but it is easier to have a bug in a line 47 syntax error in a statement ...
So yes you can do with the current PB native, but also with basic instructions that did not and 50 and I did everything. But there is a long time.
Most functions like Date (), ADDDATE (Date, Range, Value), Bin (), CopyArray (), Degree (), Hour (), ReverseString(), and so on,
anything related to MAP (), and Hascode, I had used and create bitmaps there are over 25 years ... And now it's "only" MapKey (), perfect, and so many other folows the same way... Why, because it existed before, and could do it before ... Most of what we use in the current language are only improvements, simplifications of what has been done, and that's very good ... :lol: :lol:

And when we remember a single thing that has become more complicated then it is also useful to remember...
But where? In his wishes ! And that's what I did, and that's it.
I can do everything from 20 to 50 with a few commands or functions, gadgets or ASM, but why complicate things when it can be simple ? :wink:
We could talk a long time, what is useful or not ... New or just improved ! You should know that the vast majority of our current language is actually made ​​up of former Subroutine, Function, Macro that have been reduced to one word, an expression! Program today would be redeveloped would see its number of rows multiplied by 30, 40 or more and that is what the library are first created by users and in which "x" time will become the orders "native" language of PB or in which they are ...
In short, glad of this exchange,
sincerely,
Gerhard
Sorry for my english :wink: ! (Windows Xp, Vista and Windows 7, Windows 10)
Post Reply