Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

help with records Please help!!

2»

Comments

  • RichardLRichardL Member Posts: 1
    : Ok i fixed some of the code but i still have the same problem. I tried using breakpoints and watches. They were quite useful but i still didn't find out why it gives me the file not found error. I checked that this error comes up when the computer generates 2 or more records. Here's the code. The program is set to generate one record.
    :
    : program database;
    :
    : uses crt;
    :
    : type
    :
    : subs1 = set of 0..8;
    :
    : student_record = record
    : Surname : String[2];
    : Name : String[10];
    : subs : subs1;
    : end;
    :
    : subj_record = record
    : studrecno : integer;
    : grade : array[1..3] of integer;
    : end;
    :
    : index_record = record
    : studrecno : integer;
    : Surname : String[2];
    : Name : String[10];
    : end;
    :
    :
    : const
    : MaxNoOfStudents = 30;
    : Subject_array : array [1..8] of string[4] = ('Math', 'Phys', 'SOK', 'Bio', 'Chem', 'Comp', 'Eng', 'Span');
    :
    : var
    : mainfile : file of student_record;
    : subjfile : file of subj_record;
    : indexfile,tempidx : file of index_record;
    : onestud : student_record;
    : onesubj : subj_record;
    : oneidx : index_record;
    :
    :
    :
    :
    :
    : Procedure InitDatafiles;
    : Begin
    :
    : assign(mainfile, 'C:main.DAT');
    : rewrite(mainfile);
    : close(mainfile);
    :
    : assign(indexfile,'C:index.IDX');
    : rewrite(indexfile);
    : close(indexfile);
    : end;
    :
    :
    : Procedure OpenSubjFile(Subjno : integer);
    : {opens a subject file associated with its set number SubjNo for the global variable subjfile,
    : Adds suffix '.DAT to the file name}
    : var
    : s : string[12];
    : Begin
    : s :='C:' + subject_array[SubjNo] + '.DAT';
    : Assign(subjfile, s);
    : Reset(subjfile);
    : End;
    :
    :
    : Procedure GenerateRecords(newrecs : integer);
    : var
    : i, j, k, l : integer;
    : tempsur : string[3];
    : tempname : string[10];
    : tempsubs : subs1;
    : subno, fsize, tsubno, hgrade, ygrade, avggrade : integer;
    :
    : Begin
    : tempsubs := [];
    : tsubno := 1;
    :
    : with onestud do
    : begin
    : Surname := '';
    : Name := '';
    : subs := [];
    : end;
    :
    : with onesubj do
    : begin
    : studrecno := 0;
    : end;
    :
    : with oneidx do
    : begin
    : studrecno := 0;
    : surname := '';
    : name := '';
    : end;
    :
    : assign(mainfile, 'C:main.DAT');
    : reset(mainfile);
    : For i := 1 to newrecs do
    : begin
    : tempsur := '';
    : tempname := '';
    : randomize;
    : tempsur := chr(65 + Random(26));
    : for j := 2 to 3 do
    : tempsur := tempsur + chr(65 + 32 + Random(26));
    :
    : {A = 65, B = 66, C = 67, ...)
    : (small letters = values of capital + 32}
    : randomize;
    : tempname := chr(97 + Random(26));
    : for k := 2 to 10 do
    : tempname := tempname + chr(65 + 32 + Random(26));
    :
    : for l := 1 to 3 do
    : begin
    : repeat
    : subno := 1 + random(8);
    : until not (subno in tempsubs);
    : tempsubs := tempsubs + [subno];
    : end;
    :
    : onestud.surname := tempsur;
    : onestud.name := tempname;
    : onestud.subs := tempsubs;
    : assign(mainfile, 'C:main.DAT');
    : reset(mainfile);
    : seek(mainfile, filesize(mainfile));
    : write(mainfile, onestud);
    : fsize := filesize(mainfile);
    : close(mainfile);
    :
    :
    : oneidx.studrecno := fsize;
    : oneidx.name := onestud.name;
    : oneidx.surname := onestud.surname;
    : assign(indexfile, 'C:index.IDX');
    : reset(indexfile);
    : seek(indexfile, filesize(indexfile));
    : write(indexfile, oneidx);
    : close(indexfile);
    :
    : onesubj.studrecno := oneidx.studrecno;
    : while tempsubs <> [] do
    : begin
    : if tsubno in tempsubs then
    : begin
    : opensubjfile(tsubno);
    :
    : hgrade := 1 + random(100);
    : ygrade := 1 + random(100);
    : avggrade := (hgrade + ygrade) div 2;
    :
    : onesubj.grade[1] := onesubj.grade[hgrade];
    : onesubj.grade[2] := onesubj.grade[ygrade];
    : onesubj.grade[3] := onesubj.grade[avggrade];
    :
    : write(subjfile, onesubj);
    : close(subjfile);
    : tempsubs := tempsubs - [tsubno];
    : end;
    : inc(tsubno);
    : end;
    :
    : end;
    : end;
    :
    : Procedure Lister;
    : var
    : temprec1,temprec2,temprec3,temptemp,temptemptemp : index_record;
    : flogger : boolean;
    : k,j,z,finalint,kr : integer;
    :
    : begin
    : Assign(tempidx,'c: empidx.idx');
    : Rewrite(tempidx);
    : Close(tempidx);
    : Repeat
    : Assign(indexfile,'c:index.idx');
    : Reset(indexfile);
    : for k := 0 to filesize(indexfile) - 1 do
    : begin
    : flogger := false;
    : seek(indexfile, k);
    : read(indexfile, temprec1);
    : If k + 1 < filesize(indexfile) then
    : Begin
    : seek(indexfile, k + 1);
    : read(indexfile, temprec2);
    : if temprec1.surname > temprec2.surname then
    : begin
    : flogger := true;
    : assign(tempidx,'c: empidx.idx');
    : Reset(tempidx);
    : If k > 0 then
    : Begin
    : For j := 0 to k - 1 do
    : begin
    : seek(indexfile, j);
    : read(indexfile, temprec3);
    : seek(tempidx, filesize(tempidx));
    : write(tempidx, temprec3);
    : end;
    : end;
    : seek(tempidx, filesize(tempidx));
    : Write(tempidx, temprec2);
    : Write(tempidx, temprec1);
    : For j := k + 2 to filesize(indexfile) - 1 do
    : Begin
    : seek(indexfile,j);
    : read(indexfile,temprec3);
    : seek(tempidx, filesize(tempidx));
    : write(tempidx, temprec3);
    : end;
    : close(tempidx);
    : close(indexfile);
    : Assign(indexfile,'c:index.idx');
    : Rewrite(indexfile);
    : Reset(indexfile);
    : assign(tempidx,'c: empidx.idx');
    : reset(tempidx);
    : for z := 0 to filesize(tempidx) - 1 do
    : begin
    : seek(tempidx, z);
    : read(tempidx, temptemp);
    : write(indexfile, temptemp);
    : end;
    : rewrite(tempidx);
    : reset(tempidx);
    : close(tempidx);
    : End;
    : end
    : else
    : begin
    : reset(indexfile);
    : close(indexfile);
    : end;
    : end;
    : Until flogger = false;
    :
    : Assign(indexfile,'c:index.idx');
    : Reset(indexfile);
    : Textbackground(blue); Textcolor(yellow);
    : Clrscr;
    : Gotoxy(35,4); Write('Student List');
    : kr := 0;
    : for finalint := 0 to filesize(indexfile) - 1 do
    : begin
    : kr := Kr + 1;
    : Read(indexfile, temprec3);
    : gotoxy(25,8 + kr);
    : Write(temprec3.Surname);
    : gotoxy(45,8+kr);
    : Write(temprec3.Name);
    : end;
    : close(indexfile);
    : repeat until keypressed;
    :
    : end;
    :
    : Begin
    : initdatafiles;
    : generaterecords(1);
    : lister;
    :
    : readln
    : end.
    :


    I noticed 2 things you might want to check. First, do the 8 files your trying to write to actually exist in c:? Maybes theres one missing.

    The other thing is the line:
    subno := 1 + random(8);
    Its been awhile since I worked with Pascal but could the random function return 8 and when you +1 you get 9 which is outside your array size. That would return a string that didn't match one of your files.

    The string[12] for the filename shouldn't be a problem because it looks like it will be either 10 or 11 characters including the path and extension. It would help to output the s variable in the OpenSubjFile procedure before the "assign" line. That would show you which filename is having the problem.
  • Phat NatPhat Nat Member Posts: 757
    : The other thing is the line:
    : subno := 1 + random(8);
    : Its been awhile since I worked with Pascal but could the random function return 8 and when you +1 you get 9 which is outside your array size. That would return a string that didn't match one of your files.


    This won't be a problem because the random function returns the specified number of values, so RANDOM(8) will return a number from 0-7 (8 possibilities). RANDOM(54) would return 0-53. So the RANDOM(8) part should be fine. Your comment on the files existing could be a problem, however TP would halt execution if it came across an error like that without using the [b]{I-}[/b] switch.

    Phat Nat

2»
Sign In or Register to comment.