Multiple users writing to a file

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
pj
Newbie
Posts: 5
Joined: Fri Oct 26, 2012 3:41 am

Multiple users writing to a file

Post by pj »

I have a QBasic quiz program for children to use in school.

Their results need to be written to a file on the network drive.

I am unsure what will happen if perchance more than one student attempts to write at the same time to the results file... DOES ANYONE KNOW WHAT WOULD HAPPEN?

I have considered each student having their own results file (instead of a shared results file for all students) and the teacher collating the results at a later time but all that opening and closing would make it quite slow.

I would like to find a RELIABLE, and simple solution if possible. Can anyone help?
PJ
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

Have you gotten access to the files already?

You could use ACCESS LOCK when a file is opened. Not sure how it would work though. Something about DOS SHARED.EXE having to be run.

What OS are you using? I don't think that will work on XP either.
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
pj
Newbie
Posts: 5
Joined: Fri Oct 26, 2012 3:41 am

Multiple users writing to a file

Post by pj »

Thank you for your reply...

I'm not sure what you mean by me having access to the files ... One of the other teachers will be organising the children to play the quiz on a bank of computers (as many as 10 at a time). The pupils individual scores will be recorded (written to a results file automatically on completing their quiz). Then later, at the teachers convenience, the teacher will view their results.

What concerns me, is that if 10 pupils for example try to write their results at the same time to the results file, will qBasic throw up an error and not complete the write... and if so, how to get round the problem.

I had already briefly considered ACCESS and LOCK, but after your post I have looked at it more closely and think it may provide the answer... As far as I can tell DOS SHARED.EXE is only required for the ACCESS setting. Is this correct? If this is the case, would simply setting LOCK to SHARED in the OPEN (file I/O) statement do the job for me?

I'm using qBasic 4.5 on XP and 7 and have had no problems (other than screen size restriction on 7) but as QB64 seems to have developed sufficiently to allow full screen, mouse and other functionality on these OS's, I'm thinking of modifying the code to QB64... any views?

Apologies for not acknowledging your helpful reply to my post, but once again, thank you for your time. It's a great help to be able to bounce ideas off someone and get useful suggestions.
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

If you LOCK a file some kids won't be able to write anything while it is locked.

If you don't then some data may be mixed.

The best solution would to have each kid to make a file with their names or initials so that each one can be checked later. Maybe a student number.
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
pj
Newbie
Posts: 5
Joined: Fri Oct 26, 2012 3:41 am

Multiple users writing to a file

Post by pj »

I've not been able to spend any time on my project until now, so have not got around to responding to your helpful and efficient reply... Apologies for that (It'll will probably always be like this, as writing code isn't part of my job). I have a couple of hours now.

Thanks for your suggestion about separate files for each student. I had already considered this but have swung back to the idea of one file. If I can get the file sharing to work on the network, I think it will be a lot tidier.

I have also been vacillating over the use of different file formats and have settled on RANDOM access files for the results.

I don't think that I understand all the file settings in the OPEN file statement and will probably end up learning through trial and failure. I had thought that simply setting [lock] to SHARED in the OPEN (file I/O) statement (Ref: QB45 HELP PAGE BELOW) would allow all students to share the file. Is that not the case (please comment on what it says below)?

Thanks again for all you help.

QB45 HELP
OPEN (File I/O) Statement Details

Syntax
OPEN file [FOR mode1] [ACCESS access] [lock] AS [#]filenum [LEN=reclen]

...

The lock clause works in a multiprocessing environment to restrict
access by other processes to an open file. The lock types are as
follows:

Lock Type Description
default If locktype is not specified, the file may be
opened for reading and writing any number of times
by this process, but other processes are denied
access to the file while it is opened.

SHARED Any process on any machine may read from or write
to this file. Do not confuse the SHARED lock type
with the SHARED statement or the SHARED attribute
appearing in other statements.

...
pj
Newbie
Posts: 5
Joined: Fri Oct 26, 2012 3:41 am

Multiple users writing to a file

Post by pj »

Apologies again, I don't know how I missed it but I've just noticed your warning about data being mixed up if its written by multiple users at the same time.

I had assumed that there was some sort of automatic queuing or stack system and that a complete record will be successfully written to the random access file (using TYPE and dot variables to write a complete record at a time... see incomplete code below) before allowing another record to be written from another student.

Is this not the case?

or

Do I need to try it out to see for sure?

Incomplete code ... demonstrates what I'm doing, writing a record at a time:

' The structure for each student record.
TYPE StudentRecordType
FirstName AS STRING * 20
LastName AS STRING * 20
Class AS INTEGER
Teacher AS STRING * 20
END TYPE

' Global variable declarations.
DIM SHARED Student AS StudentRecordType


' Open the student database file.
RecLength = LEN(Student)
OPEN "C:\Results.dbf" FOR RANDOM SHARED AS #1 LEN = RecLength

Student.FirstName = FirstName$
Student.LastName = LastName$
Student.Class = Yr$(R)
Student.Teacher = Teach$

PUT #1, PupilNo, Student

' Thank you!
pj
Newbie
Posts: 5
Joined: Fri Oct 26, 2012 3:41 am

Multiple users writing to a file

Post by pj »

excuse the small error...

Student.Class = Yr

should have been an integer not a string variable. (Also, there will obviously be a Student.Score variable included in the declaration)... It's not strictly relevant but I didn't want it to divert from the question...

... Will the complete record be written (whatever form it takes) without mixing up the contents of different records if they're attempting to write at the same time?

I can easily use the student names and class or some record key matched to another file that holds student details to sort the file holding records of scores, as long as the individual records in the file are intact.
Post Reply