I need help with a program for school. I am a very novice pascal programmer. I need to allow the person using the program to add a record and then search for it and view it. Currently, whenever I add a record via the program and try to search for it and view it, the same record shows up.

This first program is the program I used to create the file.

        PROGRAM SpellingBeeFile;

           USES
              CRT, SYSUTILS;

           TYPE
              TestRec = RECORD
                           TestNo      : LONGINT;
                           Word        : ARRAY[1..10] OF STRING[15];
                           Definition  : ARRAY[1..10] OF STRING[40];
                        END;

              TestFileRqd = FILE OF TestRec;

           VAR
              Test : TestRec;
              TestFile : TestFileRqd;

           PROCEDURE InitialFile;
              BEGIN
                 ASSIGN(TestFile,'C:\TestFile.dta');
                 REWRITE(TestFile);
                    WITH Test DO
                       BEGIN
                          Test.TestNo := 1;

                          Word[1] := 'hectare';
                          Definition[1] := 'unit of area';

                          Word[2] := 'active';
                          Definition[2] := 'being busy';

                          Word[3] := 'amaze';
                          Definition[3] := 'surprise';

                          Word[4] := 'bound';
                          Definition[4] := 'leap and jump';

                          Word[5] := 'cavern';
                          Definition[5] := 'a large cave';

                          Word[6] := 'difficult';
                          Definition[6] := 'hard to do';

                          Word[7] := 'frozen';
                          Definition[7] := 'turned to ice';

                          Word[8] := 'garage';
                          Definition[8] := 'building for a car';

                          Word[9] := 'jaguar';
                          Definition[9] := 'large cat';

                          Word[10] := 'lemonade';
                          Definition[10] := 'fizzy drink'; 
                          WRITE(TestFile, Test);
                       END;
                 CLOSE (TestFile);
              END;

           BEGIN
              InitialFile;
           END.

This is the program used to maintain the file created by the first program.

        PROGRAM SpellingBee;
           USES
              CRT, SYSUTILS;

           TYPE
              TestRec = RECORD
                           TestNo      : LONGINT;
                           Word        : ARRAY[1..10] OF STRING[15];
                           Definition  : ARRAY[1..10] OF STRING[40];
                        END;

              TestFileRqd = FILE OF TestRec;

           VAR
              Test : TestRec;
              TestFile : TestFileRqd;
              MenuChoice, WD, AnotherTest : CHAR;
              Choice : LONGINT;
              pos,pos1 : INTEGER;
              T : LONGINT;

           PROCEDURE DisplayTest;
              BEGIN
                 RESET(TestFile);
                 CLRSCR;
                 READ(TestFile, Test);
                 WRITELN('Test Number: ', Test.TestNo);
                 WRITELN;
                 FOR pos := 1 TO 10 DO
                    BEGIN
                       WITH Test DO
                          BEGIN
                             WRITELN(pos,') Word : ',Word[pos]);
                             WRITELN('   Definition: ',Definition[pos]);
                             WRITELN;
                          END;
                    END;
                 WRITE('Press Enter to continue...');
                 READLN;
              END;     

           **PROCEDURE TestSearch;
              VAR
                 Search : LONGINT;
                 Found  : BOOLEAN;

              BEGIN
                 CLRSCR;
                 RESET(TestFile);
                 WRITELN('Enter Test Number: ');
                 WRITELN;
                 WRITELN('Latest Test Number: ', FILESIZE(TestFile));
                 GOTOXY(20,1);
                 READLN(Search);

                 Found := FALSE;
                 READ(TestFile,Test);
                 IF FILESIZE(TestFile) = 0 THEN
                    BEGIN
                       WRITELN('No tests have been saved');
                    END;

                 SEEK(TestFile, Search - 1);
                 IF Test.TestNo = Search THEN
                    BEGIN
                       Found := TRUE;
                       DisplayTest;
                    END;

                 IF NOT (Found) THEN
                    BEGIN
                       CLRSCR;
                       WRITELN('Error - No tests were found');
                       WRITELN;
                       WRITELN('Press Enter to continue...');
                       READKEY;
                    END;
              END;**

           PROCEDURE WrdOrDef;
              BEGIN
                 CLRSCR;
                    WRITE('Change word or definition? (W/D)');
                    WD := UPCASE(READKEY);

                    CASE WD OF
                       'W' : BEGIN
                                CLRSCR;
                                WRITELN('Old Word: ', Test.Word[Choice]);
                                WRITELN;
                                WRITE('Enter new word: ');
                                READLN(Test.Word[Choice]);
                             END;   
                       'D' : BEGIN
                                CLRSCR;
                                WRITELN('Old Definition: ', Test.Definition[Choice]);
                                WRITELN;
                                WRITE('Enter new definition: ');
                                READLN(Test.Definition[Choice]);
                             END;   
                    ELSE
                       WRITELN('Only enter W or D please..')
                    END;
              END;           

           PROCEDURE AmendTest;
              VAR
                 WD : CHAR;
                 Again : CHAR;

              BEGIN
                 REPEAT
                    TestSearch;
                    WRITELN;
                       GOTOXY(40,4);
                    WRITELN('Enter the number of the');
                       GOTOXY(35,5);
                    WRITELN('word/definition you wish to amend: ');
                       GOTOXY(70,5);
                    READLN(Choice);

                    CASE Choice OF
                       1  : BEGIN
                               WrdOrDef;  
                            END;
                       2  : BEGIN
                               WrdOrDef;
                            END;
                       3  : BEGIN
                               WrdOrDef;
                            END;
                       4  : BEGIN
                               WrdOrDef;
                            END;
                       5  : BEGIN
                               WrdOrDef;
                            END;
                       6  : BEGIN
                               WrdOrDef;
                            END;
                       7  : BEGIN
                               WrdOrDef;
                            END;
                       8  : BEGIN
                               WrdOrDef;
                            END;
                       9  : BEGIN
                               WrdOrDef;
                            END;
                       10 : BEGIN
                               WrdOrDef;
                            END;
                    ELSE
                       WRITELN('Only enter numbers 1 - 10');         
                    END;

                    SEEK(TestFile, FILEPOS(TestFile) - 1);
                    WRITE(TestFile,Test);
                    CLRSCR;
                    DisplayTest;
                    READLN;
                    CLRSCR;
                    WRITELN('Stop Amending Tests?');
                    WRITELN;
                    WRITELN;
                    WRITELN('Y - Yes');
                    WRITELN;
                    WRITELN('Otherwise, press any  other key to add another test');
                    Again := UPCASE(READKEY);       
                 UNTIL Again = 'Y';
              END;  

           **PROCEDURE AddTest;
              BEGIN
                 CLRSCR;
                 RESET(TestFile);
                 SEEK(TestFile,FILESIZE(TestFile));

                 Test.TestNo := 2;
                 FOR pos1 := 1 TO 10 DO
                    BEGIN
                       CLRSCR;
                       WRITELN('Enter Word ',pos1,': ');
                       READLN(Test.Word[pos1]);
                       WRITELN('Enter Definition ',pos1,': ');
                       READLN(Test.Definition[pos1]);
                    END;
                 WRITE(TestFile,Test);        
              END;**       


           BEGIN
              ASSIGN(TestFile,'C:\TestFile.dta');

              WRITELN('Welcome to the test update program');
              WRITELN;
              WRITE('Press any key to continue...');
              READLN;

              REPEAT
                 CLRSCR;
                 WRITELN('A: Search for a test');
                 WRITELN('B: Amend a test');
                 WRITELN('C: Add a test');
                 WRITELN;
                 WRITELN('E: Quit');
                 WRITELN;
                 WRITE('Enter Choice...');
                 MenuChoice := UPCASE(READKEY);

                 CASE MenuChoice OF
                    'A' : TestSearch;
                    'B' : AmendTest;
                    'C' : AddTest;
                 ELSE
                    WRITELN('Error - only enter A-E. Try Again.');
                 END;

              UNTIL MenuChoice = 'E';

              CLOSE(TestFile);
           END.

I have made bold the two procedures that are most likely to be causing this. Thank you in advance for the help

Dani AI

Generated

Good spot by and well observed, — the symptom you described is exactly what you found: the display routine resets the file and performs a read, which moves the file pointer and makes the wrong record appear when you call it from the search routine. File I/O in Pascal is positional: calling RESET or READ changes the file position, so the caller and callee must agree who is responsible for positioning the file before reading.

Practical fixes (pick one):

  • Make the display procedure a pure presenter: remove any RESET/SEEK/READ from it and have it accept a TestRec (the already-read record) to print. Then the search routine should RESET, validate that the requested index is between 1 and FILESIZE, SEEK(Search-1), READ into a record, and call the display procedure with that record.
  • Or make the display procedure accept a record index and be responsible for positioning and reading that index — but do not call RESET inside the display routine if the caller already opened the file. Keep responsibility for opening/closing consistent.

Additional robustness notes:

  • Always check FILESIZE before seeking or reading; handle empty files explicitly.
  • Do not perform a READ before you SEEK if you intend to examine a specific record — position first, then read.
  • When appending, compute the test number from FILESIZE (e.g., new number = FILESIZE + 1) instead of hard-coding values.
  • When amending, overwrite using SEEK(FILEPOS - 1) followed by WRITE to replace the current record.

Quick checklist to verify after changes: open file once, validate requested index, position (SEEK), read, display (without resetting), and only then close. This keeps file position predictable and avoids the duplicate/wrong-record symptom you were seeing.

Recommended Answers

All 3 Replies

I tried to bold the procedures but it did not work. Procedures 'TestSearch' and 'AddTest' are the procedures in question. They are in the second program

In the TestSearch procedure you SEEK to the last record in the file but you don't read that record before checking the Test.TestNo

Thanks so much but I figured out what was wrong. In testsearch, I call for display test and in displaytest, there is a READ which moves the file pointer to the next record and so displays a different test.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.