Editing files

I've got a file of the following type:

TYPE Glostyp = RECORD
  SveOrd : String[25];
  UtlOrd : String[25];
  Korrekt : Boolean;
END;

glosfil : FILE OF Glostyp;

When my program starts it reads in all the words from the file in an array of glostyp:
glosor : ARRAY[1..200] OF Glostyp;

The user is then prompted to specify a search word and glosor[i] that matches the searchword shows on screen.
If the users chooses to delete glosor[i] the word should also be deleted from the file, how do I accomplish this?
The order in the file can't be messed up, it has to be the same as it was in the beginning, except for the deleted word.
PsychoFoxAsked:
Who is Participating?
 
BatalfConnect With a Mentor Commented:
Hei

I agree with Hypo. You should remove the entry from the glosor array and then rewrite the glosfil.

To delete the word from the array you should use a for-structure

ex : the word you want to delete is number 'n' in the array.

for i:=n to 199 do
glosr[i]:=glosr[i+1];

then, you should rewrite the file :

assignfile(glosfil,<path>);
rewrite(glosfil);
write(glosfil, glosr);
close(glosfil);


the structure would then be like it was, except from the deleted word.

It could also be useful to have a total-integer which tell you how many words you have in your array. When you add a word, the total would increase by one, and when you delete on, it would be decreased by one. Then you don't have to search through empty entries.

Hope my answer would help you.

Batalf
0
 
HypoCommented:
Can't you just rewrite the file at shutdown whenever a change to the "glosor list" has been made?
e.g. If you remove an item in the array remake the list so that all valid items (glosor) follow eachother in the correct order... Then, before you close the program, just overwrite the whole file with the altered array.

(Is there any reason for the Korrekt variable within the Glosor Type to be in the file???)
0
 
PsychoFoxAuthor Commented:
Thanks to both of you, I'll check this out.

I have already have a total word-count :)

(The korrekt variable is should be there, it keeps record of which words were correct and which were not (this is a part of a word-knowledge program))
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
PsychoFoxAuthor Commented:
Thanks to both of you, I'll check this out.

I have already have a total word-count :)

(The korrekt variable is should be there, it keeps record of which words were correct and which were not (this is a part of a word-knowledge program))
0
 
scrapdogCommented:
assign some arbitrary value, such as "xxxx", to the SveOrd of the glosor you want to delete (or some value you are sure that will never occur otherwise).

Then when you rewrite the file, simply skip over (don't write) the records that contain a SveOrd of "xxxx" (or whatever value you choose)
0
 
BatalfCommented:
Have you tried some of our tip?
0
 
geteCommented:
An addition to scrapdog, to prevent losing changes caused by e.g. power blackout (or somehow the computer got hang in the middle of the running program), make change to the record in the file too, so if something happened, you don't lose the deletion made in previous session. At the end of the session, you could recreate the file skipping the "marked" record. It wouldn't take too much time to perform the rewriting (unless the size is really huge), or if size doesn't matter to you, just leave the deleted record, in the next sessions when you open and read it, skip the "marked" record. You can optionally make a function similar as "PACK" in old x-Base apps to physically delete all the "marked" records.

Regards, Gt
0
 
PsychoFoxAuthor Commented:
Thank you!
I was on holiday for some time and I didn't have any time to look at you answers.
0
All Courses

From novice to tech pro — start learning today.