How to Write Record into a file?

In this category you can exchange your programming questions and solutions.
Post Reply
Manolo
Posts: 16
Joined: Fri Jun 15, 2007 4:57 pm

How to Write Record into a file?

Post: # 737Post Manolo
Fri Jul 22, 2011 5:48 pm

I tried to write a minimal record into a file but I got just Errors.
Is there someone that can explain me how do it?  :(

Tnx.

Marco
Site Admin
Posts: 246
Joined: Sat Sep 15, 2018 8:41 pm

How to Write Record into a file?

Post: # 738Post Marco
Fri Jul 22, 2011 7:50 pm

I hope this will help you,best regards:

record car
? wheel as integer
? mirror as string
end record

`put som data in it
car.wheel=10
car.mirror="good"

`declare and open filestream
dim data as filestream
data.open("test.txt")

`write data
data.write(car)

``close file
data.close


Manolo
Posts: 16
Joined: Fri Jun 15, 2007 4:57 pm

How to Write Record into a file?

Post: # 739Post Manolo
Sat Jul 23, 2011 8:05 am

Hooo tnx a lot Marco...
I tried with RANDOMFILE but it is the wrong way probably.
but... how works RANDOMFILE?

Marco
Site Admin
Posts: 246
Joined: Sat Sep 15, 2018 8:41 pm

How to Write Record into a file?

Post: # 740Post Marco
Sat Jul 23, 2011 3:56 pm

yes, forgot the powerfull randomfile,
here`s an example of it
you can address the records by a number and read and write whole records at once. I think this is preferable.
here`s some sample code to experiment with it.
best regards

`==========set it up============
record car
  wheel as integer
  color as string * 10
end record

dim dbase as randomfile
`open file this before assigning the record
dbase.open("database.txt")


`assign record
dbase.assign(car)
`===============================

`put some data
car.wheel=10
car.color="red"

`append to file
dbase.append

`==========check the results====
car.wheel=0
car.color=""

`read record 1 yuo can also write a record with put
dbase.get(1)

showmessage "found "+car.color+str$(car.wheel)
`===============================

Cage
Posts: 2
Joined: Thu Dec 01, 2011 4:40 am

How to Write Record into a file?

Post: # 741Post Cage
Thu Dec 01, 2011 5:07 am

I just recently downloaded the newest version of FNX and discovered a really good programing tool.? Being a VB programer for years, I was looking for a good tool to replace it since VB no longer is supported.? I have a bunch of programing tools, but FNX is quickly turning into my favorite.? Basically with the addition of the RANDOMFILE FNX has become a viable programing tool for me.? ?My thanks to YaBB for his basics in using the random file made my transition much easier.? ?After one evening of playing with his example I had figured out all the random file commands.? ?Thank you Yabb? :)

Hope you don`t mind me posting the expanded version.

`*********************RANDOM FILE ********************
`This program explains the basics for use of the random
`file method.? ?It`s not a complete program on all the
`differnt methods of using a random database but it will
`get you started.?

Dim a as INTEGER,i as INTEGER


`First set up the record varibles.?
`In other basics they used the TYPE
`and END TYPE.? Works the same just
`called record instead.

record car
? wheel as string *6
? color as string * 10
end record


`Assign a name to the random file
`in this case we will call it dbase
dim dbase as randomfile

`open file before assigning a record
`Give the Database a name in this case
`I`ll call it DATABASE.DBF (DBF=Data Base File)
dbase.open("database.dbf")


`Assign the record called car to the database
dbase.assign(car)

`Assign some data to place in the dbase record file
car.wheel="10"
car.color="red"

`Note: Records in the database always start
`with 0, so the first record wour be 0

`Now put the information to record 0
dbase.put(0)

`Clear the car data for next record
car.wheel=""
car.color=""


`Assign some data for the next record
car.wheel="1"
car.color="blue"

`Put the information into the next record 1
dbase.put(1)

`Now we will sort the records to put
`them in order using car.wheel.
dbase.Sortfields(car.wheel)



Cage
Posts: 2
Joined: Thu Dec 01, 2011 4:40 am

How to Write Record into a file?

Post: # 742Post Cage
Thu Dec 01, 2011 5:08 am

This is the second half.

`Before you read back the data, get the number of
`records in the database. Assign this number to
`a varible, in this case a

a=dbase.records

`Now print the number of records
Print "Number of Records ";a

`Set the pointer i to equal 0
i=0


`Now get and display the records.
`While i (pointer) is less then
`the number of records in a
While i<a
     
     `Clear the varibles for car
     car.wheel=""
     car.color=""

     `Now get the records in the database and display them
     dbase.get(i)
     Print "Record: ";i;"? Wheel: "; car.wheel; " Color: ";car.color
     i=i+1
Wend

Print
Print
Print
Print

`Now I will swap the records back to the
`way they were entered originally.
`Swapping record 1 with record 0.
`This is a good method to fill in the
`gaps in your database
dbase.Swaprec(1,0)

`Set the pointer I to equal 0
i=0


`Now get and display the records.
While i<a
     
     `Clear the varibles for car
     car.wheel=""
     car.color=""

     `Now get the records in the database and display them
     dbase.get(i)
     Print "Record: ";i;"? Wheel: "; car.wheel; " Color: ";car.color
     i=i+1
Wend

Print "dbase is closed"

`Close the database
dbase.Close

`Wait for a key press to end program
Print
Print "Press a key"
While inkey$="":wend



`===============================
`Us the delete command to delete records
`dbase.Delete(recordnumber)
`Note: information in the record will be
`deleted not the actual record. The
`record will just be a blank.
`To insert new data to a blank record
`use the following command.
`dbase.Insert(recordnumber)
`you would scan the records to find the empty
`record, and use the record number found to
`inset the new data.



Post Reply