Qbasicnews.com

Full Version: Another Question - File opening and writing
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4
Hello all,

1st let me say thanks to all of those that have helped me with my previous posts.

Now onto my new problem. :???:
With the program I am writing, I would like to be able to open what I call a running file and have the program add to it. Basically it would work like this. I will ask the user several questions. For this example lets us name, age. After I have the information, I want to be able to open a file that has the same information from other people and then add the new information to the list. And then have the computer assign the person a number based off a number in the opened file. So if the user is the 10th person entering the information, the program would assign him the number 10 and the next person to use the program would get assigned 11.

I would like the file to look something like this.
""""""""""""""""""""""""""""""""""""""""""""""""
Number (assigned) Name Age
** Chris 22
ect....

""""""""""""""""""""""""""""""""""""""""""""""""

Also, I will need to be able to search this file for key words later on and list any information based on that search.

So if I search for the person with the number 15, I will get the following information

Number - Name - Age

Also if I search for anyone with the age of 22, I would recieve all persons of that age in the above format.

Then I need to save the file and exit the program.

Everytime this program is run I need it to open and get the last number added and then assign the next number in order. I would like it to allow alphanumerics. So the first number would start with A1 and the go to A2 and so on.

This might sound confusing, please if you have questions let me know. I would appreciate any input.
I might be confused as to what you're asking, but here's a way to save a simple flatfile database in qb.

Code:
TYPE data
  name as string * 15 ' this string can only be 15 chars long
  age as integer
  petsname as string * 15
END TYPE

' to write a record
DIM recordx as data
recordx.name = "Jane Doe"
recordx.age = 25
recordx.petsname = "Jofers"

open "datafile.dat" for binary as 1
  seek lof(1) + 1
  put #1, , recordx
close 1

' to retrieve records

dim records(100) as data ' max: 100 entries.

open "datafile.dat" for binary as 1
  do until eof(1)
    get #1, , records(n)
    numrecords = numrecords + 1
  loop
close 1

of course, this is off the top of my head and may not work. but that's how a flatfile db ought to be done in qb.
Quote:TYPE info
personsName as string * 15 ' this string can only be 15 chars long
age as integer
petsname as string * 15
END TYPE

' to write a record
DIM recordx as info
recordx.PersonsName = "Jane Doe"
recordx.age = 25
recordx.petsname = "Jofers"

open "datafile.dat" for binary as 1
seek #1, lof(1) + 1
put #1, , recordx
close 1

' to retrieve records

dim records(100) as data ' max: 100 entries.

open "datafile.dat" for binary as 1
do until eof(1)
get #1, , records(n)
numrecords = numrecords + 1
loop
close 1
Added and ammended parts of code. (NAME and DATA are reserved.)

If you wan't to search the file, without loading the whole file into an array, you could use this.
Code:
'this code assumes you have alread defined recordx

OPEN "datafile.dat" FOR BINARY AS #1
   numberOfRecords% = LOF(1) / LEN(recordx)
   FOR record% = 1 to numberOfRecords%
      GET #1, record%, recordx

      'each of the strings are saved as 15 characters,
      'so it would normaly equal "Jane Doe       "
      IF RTRIM$(recordx.personsName) = "Jane Doe" THEN
         REM do what you want with it
      END IF
   NEXT record%
CLOSE #1
You will obviously have to add in your own parts of code to decide what part of the record to search, what to search for, and what happenes when you find something.
thanks -- i forgot about that Tongue

quick note: numrecords is the number of records after the database has been loaded.
quick note: I noticed you dont load the entire thing. gotcha.
lol
Typed by heart right?
I think you guys understand what I want, But can someone please explain to me what is writen in the code that appears. I am really confused as to what is going on. I guess I want to understand each step along the way. Kind of like this.

"""""""""""""""
' to write a record
DIM recordx as info
recordx.PersonsName = "Jane Doe"
recordx.age = 25
recordx.petsname = "Jofers"
"""""""""""""""

I am not sure what this means.
Thanks for bearing with me. It has been so long since I programmed last.

Thanks
Chris
Maybe someone could email me some programs that use the .dat files in a similar situation as to what I am looking for that way I could see exactly how they are used in a program. Just and Idea.
If so, email to csonon@ironmount.biz

Thanks all
There are basicaly two types of file you can have, sequential and random access.
Sequential files are writtn to and read from in order, from begenning to end (like a tape)
Random access files can be read from and written to at any point (like a CD)

Sequential access files are only really usefull for logs or times when the file isn't going to change often, and be read through from begenning to end.

A Random access file is more usefull for when you want a flatfile database of something. (like a phone book)

A Random access file holds information in RECORDS, a record is a group of information (like name and address) all combined into one.
E.G.
Code:
Phydaux        4 My street                   (029) 20893232
is 1 record containing three strings of lengths 15, 30, 15 respectivly.

This record is a specific DATA TYPE (like an integer, or string) and must be defined.
Code:
TYPE info
   first AS STRING * 15
   street AS STRING * 30
   telephone AS STRING * 15
END TYPE
This defines the data type info. To use these in a variable you need to declare the variable. You cannot use info like a variable, as it would be as silly as saying LET INTEGER = 4.

Code:
DIM person AS info
if you wanted to hold info on more than one person at a time you would just define person as an array.
Code:
DIM person(1 TO 100) AS info


You cannot just write information directly to the person variable (unless the data passed to it is already in the correct format, like reading from a file (but we'll get to that)) but you can write to the different parts like you would a normal variable.
Code:
person.first = "Phydaux"
INPUT "Your street is"; person.street

to save this stuff to a file, you first need to open one. N.B. if the file doesn't exist then it will automaticaly be created.
Code:
OPEN "address.book" FOR RANDOM AS #1 LEN = LEN(person)
this opens up the file "address.book" with RANDOM access, as file number 1, and declares that each record is the length of the person varable.

Now the file is open you can write to it. To write to a file you use the PUT command, where you can specify which file and record to write to, and what to write.
Code:
PUT #1, recnum%, person
This writes all the information stored in person into the recnum% record.
deciding which record to write to is up to you.

To GET information from a file, we use the GET command. It uses exactly the same syntax as the put command.

After we are done with the file make sure to close it or other programs wont be able to access it.
Code:
CLOSE #1
END and SYSTEM also close all open files, but thats a bit lazy and can make it hard to follow a programs layout.

This should be a whole working program randomly accessing the file.
Code:
TYPE info
   first AS STRING * 15
   street AS STRING * 30
   telephone AS STRING * 15
END TYPE

DIM person AS info

OPEN "address.book" FOR RANDOM AS #1 LEN = LEN(person)
   DO
      'this finds how many records there are
      numberOfRecords% = LOF(1) / LEN(person)
      CLS
      PRINT "1) Write info"
      PRINT "2) Read info"
      PRINT "3) Change info"
      PRINT
      PRINT "4) Quit"
      DO
         key$ = INKEY$
      LOOP WHILE VAL(key$) < 1 OR VAL(key$) > 4

      SELECT CASE VAL(key$)
         CASE 1
            'adds information to the file

            INPUT "Name   :", person.first
            INPUT "Street :", person.street
            INPUT "Tel.   :", person.telephone
            PUT #1, numberOfRecords% + 1, person
         CASE 2
            'this will find all the people who share the same first name
            'it can easly be changed to search other parts of the record

            INPUT "Whos information do you want to find"; who$
            FOR record% = 1 TO numberOfRecords%
               GET #1, record%, person
              
               IF RTRIM$(person.first) = who$ THEN
                  PRINT "Name   :"; person.first
                  PRINT "Street :"; person.street
                  PRINT "Tel.   :"; person.telephone
                  PRINT
               END IF
            NEXT record%
            PRINT "press any key to continue..."
            DO: LOOP UNTIL INKEY$ <> ""

         CASE 3
            'this will find all the people who share the same first name
            'and change the information about them

            INPUT "Whos information do you want to change"; who$
            FOR record% = 1 TO numberOfRecords%
               GET #1, record%, person
               IF RTRIM$(person.first) = who$ THEN
                  INPUT "Name   :", person.first
                  INPUT "Street :", person.street
                  INPUT "Tel.   :", person.telephone
                  PUT #1, record%, person
               END IF
            NEXT record%
      END SELECT
   LOOP UNTIL key$ = "4"
CLOSE #1
END
You should obviously refine and talor this to your own needs Smile HTH
Ok I think I somewhat but not really understand what you are saying. But here is some code that I entered into my program based off what you told me. But I get an error when I try and run the program. The error says "excpected: expression". No clue what that means. Here is the code.

""""""""""""""""""""""""""""""
TYPE INFO
NAME AS STRING * 15
DATE AS STRING * 10
PNAME AS STRING *40
PRNAME AS STRING *30
PROJECT AS STRING *15
END TYPE
DIM LOG AS INFO
OPEN "C:\PART.LOG" FOR RANDOM AS #1 LEN = (LOG)
DO
NUMBEROFPARTS% = LOF(1) / LEN(LOG)
PUT #1, NUMBEROFPARTS% + 1, LOG
CLOSE#1
END
""""""""""""""""""""""""""""""

thanks
Chris
I find the random mode rather... sucky. I've always preferred binary because it gives you more control. But that's just me.

it's giving you that error because LOG is a function. You can't name your variable that.
Pages: 1 2 3 4