Recent

Author Topic: [Solved]Update / Delete from text file  (Read 2534 times)

snowpl

  • Newbie
  • Posts: 2
[Solved]Update / Delete from text file
« on: November 22, 2017, 01:56:07 pm »
Hello there,
I've got some academic project (have to make some kind of DataBase application so simply we need to show records, allow to add, update and delete).

Code: Pascal  [Select][+][-]
  1. unit BeastsOpeations;
  2.  
  3. interface
  4. uses
  5.  SysUtils, classes;
  6.  
  7. TYPE
  8.  
  9.   T_Beast = RECORD
  10.     ID, HP, Atk: Integer;
  11.     Name: string;
  12.     end;
  13.  
  14.   VAR
  15.     plik: text;
  16.     s, newBeastInfo: string;
  17.     i, beastsCount, BeastId: integer;
  18.     sl: TStringList;
  19.     newBeast: T_Beast;
  20.  
  21.  PROCEDURE ShowAllBeasts();
  22.  PROCEDURE ShowCommands();
  23.  FUNCTION CountAllBeasts():integer;
  24.  FUNCTION ShowAndCountAllBeasts(): integer;
  25.  PROCEDURE AddNewBeast();
  26.  PROCEDURE RemoveBeast();
  27.  
  28. implementation
  29.  
  30. PROCEDURE ShowAllBeasts();
  31. BEGIN
  32.     WriteLn('Obecne potwory:');
  33.     ASSIGN(plik, 'bestie.txt');
  34.     RESET(plik);
  35.     REPEAT
  36.     ReadLn(plik, s);
  37.     writeLn(s);
  38.     until EOF(plik);
  39.     ClOSE(plik);
  40. END;
  41.  
  42. PROCEDURE AddNewBeast();
  43. BEGIN
  44.     beastsCount:= ShowAndCountAllBeasts();
  45.     ASSIGN(plik, 'bestie.txt');
  46.     APPEND(plik);
  47.     WriteLn('Podaj nazwe nowej bestii');
  48.     ReadLn(newBeast.Name);
  49.     WriteLn('Podaj ilosc HP ', newBeast.Name);
  50.     ReadLn(newBeast.HP);
  51.     WriteLn('Podaj Atak bestii ', newBeast.Name);
  52.     ReadLn(newBeast.Atk);
  53.     Inc(beastsCount);
  54.     newBeast.ID := beastsCount;
  55.     newBeastInfo := IntToStr(newBeast.ID) + ' ' + newBeast.Name + ' '
  56.     + IntToStr(newBeast.HP) + ' ' + IntToStr(newBeast.Atk);
  57.     WriteLn(plik, newBeastInfo);
  58.     CLOSE(plik);
  59.     WriteLn('Nowa bestia zostala pomyslnie dodana');
  60. end;
  61.  
  62. PROCEDURE RemoveBeast();
  63. BEGIN
  64.     beastsCount:= ShowAndCountAllBeasts();
  65.     //ASSIGN(plik, 'bestie.txt');
  66.     //APPEND(plik);
  67.     WriteLn('Podaj ID bestii ktore chcesz usunac');
  68.     ReadLn(BeastId);
  69.     sl:=TStringList.Create;
  70.     try
  71.     sl.LoadFromFile('bestie');
  72.     sl.NameValueSeparator:= ' ';
  73.     if(BeastId <= beastsCount) THEN
  74.     BEGIN
  75.         sl.Delete(BeastId);
  76.         sl.SaveToFile('bestie.txt');
  77.         //sl.free;
  78.     end
  79.     ELSE
  80.     BEGIN
  81.          WriteLn('Podano niepoprawne ID bestii');
  82.          //sl.free;
  83.     END;
  84.     finally
  85.       sl.free;
  86.     end;
  87. end;
  88.  
  89. FUNCTION CountAllBeasts(): integer;
  90. BEGIN
  91.      ASSIGN(plik, 'bestie.txt');
  92.      RESET(plik);
  93.      beastsCount:=0;
  94.      REPEAT
  95.      ReadLn(plik);
  96.      Inc(beastsCount);
  97.      until EOF(plik);
  98.      CLOSE(plik);
  99.      Dec(beastsCount);
  100.      result := beastsCount;
  101. END;
  102.  
  103. FUNCTION ShowAndCountAllBeasts(): integer;
  104. BEGIN
  105.      ASSIGN(plik, 'bestie.txt');
  106.      RESET(plik);
  107.      beastsCount:=0;
  108.      REPEAT
  109.      Inc(beastsCount);
  110.      ReadLn(plik, s);
  111.      writeLn(s);
  112.      until EOF(plik);
  113.      CLOSE(plik);
  114.      Dec(beastsCount);
  115.      result := beastsCount;
  116. END;
  117.  
  118. end.
  119.  

The code shows and adds rows with no bigger problem (I'm not sure about the correctness of this solution). But the removing is not working. If you could give me some advices how to do it. When finished with removing just an advice for how to take care of update would be great too.
Thanks for helping.
« Last Edit: November 22, 2017, 04:40:08 pm by snowpl »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11384
  • FPC developer.
Re: Update / Delete from text file
« Reply #1 on: November 22, 2017, 02:11:45 pm »
The only solution to remove or insert data is creating a new file, copy the contents except for the parts to be deleted.


snowpl

  • Newbie
  • Posts: 2
Re: Update / Delete from text file
« Reply #2 on: November 22, 2017, 03:11:02 pm »
 :'( So as I understand I should do it like this:
1. Create new file
2. Create some array of objects from previous file
3. Iterate by my array and change/remove selected rows by id.
4. Then Iterate again if removed and changed the Id value of each (I want the ID == record number)
5. Save the array of objects into my new file.
6. Remove old file
7. Rename new file into old name.

The duplicated code which would be created I should export into interior functions and reuse it in edit and remove functions right?

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11384
  • FPC developer.
Re: Update / Delete from text file
« Reply #3 on: November 22, 2017, 03:26:03 pm »
Yes, that's about it.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Update / Delete from text file
« Reply #4 on: November 23, 2017, 12:59:16 pm »
:'( So as I understand I should do it like this:
Basically yes.

Another option (at least for removal) is to have a status field for each record, and mark a entry for deletion so that you can 'skip' that record (and re-use the record amrked for deletion when adding records).

However, this add a fair amount of complexity especially when you also want to have an ascending record ID without skipping number ID's. In that case it would probably be easier to just use an already existing database solution.

 

TinyPortal © 2005-2018