Recent

Author Topic: [SOLVED]how to write a .dat file part by part  (Read 7745 times)

questionstoaskislot

  • New Member
  • *
  • Posts: 16
  • I need to ask as much as questions I Can..
[SOLVED]how to write a .dat file part by part
« on: August 02, 2017, 04:23:34 am »
Hello people :)

I have another stupid question

I need to write a .dat file part by part I mean
Like this

There are 40 parts in that file right
Mean this program is a something which can store something and read it back
Its storage is 40.
But the string that user inputs is 'unknown'
I need to write something like this
(This one never works)

Userinputdata.da1:=(data){something user gives}
Blockwrite(f,userdata(storage)){there are 40 parts in userdata record so storage = part number ex:- userd1,userd2}
How to do that
Or is there a any way to continue writing again from that stopped part without writing them all back and without wiping out data
« Last Edit: August 02, 2017, 09:42:47 am by questionstoaskislot »

Handoko

  • Hero Member
  • *****
  • Posts: 5154
  • My goal: build my own game engine using Lazarus
Re: how to write a .dat file part by part
« Reply #1 on: August 02, 2017, 06:41:28 am »
Or is there a any way to continue writing again from that stopped part without writing them all back and without wiping out data

Yes, you can append data to a file without wiping out the previous data. You can use database, which is usually easier or direct accessing the file. Here has tutorials about how to save/retrieve/append data to file and dbf database:

http://wiki.freepascal.org/File_Handling_In_Pascal
http://wiki.freepascal.org/Lazarus_Tdbf_Tutorial

These 2 above are the very basic, after you understand them then you can try other databases or methods.
« Last Edit: August 02, 2017, 06:48:45 am by Handoko »

questionstoaskislot

  • New Member
  • *
  • Posts: 16
  • I need to ask as much as questions I Can..
Re: how to write a .dat file part by part
« Reply #2 on: August 02, 2017, 06:50:34 am »
So you mean after the command
Blockwrite(or writeblock :D)
They save the record's variabels which are <> ''
And we can start writing from that stopped place

If you can please put a example code I'll put my record code

Handoko

  • Hero Member
  • *****
  • Posts: 5154
  • My goal: build my own game engine using Lazarus
Re: how to write a .dat file part by part
« Reply #3 on: August 02, 2017, 06:57:44 am »
I rarely use blockwrite/read, I use database. So I think you won't be interested to know.

Try to search 'append' in the tutorial of the first link I gave you, there has example of appending data.

questionstoaskislot

  • New Member
  • *
  • Posts: 16
  • I need to ask as much as questions I Can..
Re: how to write a .dat file part by part
« Reply #4 on: August 02, 2017, 07:19:03 am »
This is my programme code
As the programme was a bit long i edited it

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: how to write a .dat file part by part
« Reply #5 on: August 02, 2017, 07:57:12 am »
I would do .dat files with TFileStream (or TMemoryStream). It's easier to move in the file and read or write where you want any kind of mixed data.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: how to write a .dat file part by part
« Reply #6 on: August 02, 2017, 08:00:03 am »
have you read the help on rewrite? Did you noticeed that it says the file is trancated to 0 size if it exists? Did you tried the append function as hinted by handoko? Did it not work?
In short you can't use rewrite to append more data at the end of the file you need to either open it using append or openfile. One more comment that has nothing to do with your question. you can't save an ansi string as a record, you are saving only the pointers to the ansi strings which are only valid as long as your application runs at best.
Try to save a single string first something along the lines of
Code: Pascal  [Select][+][-]
  1. var
  2.   vStr : ansistring;
  3.   vHndl:File;
  4. begin
  5.   vStr := 'This is a dynamic string you can''t save like a short string';
  6.   assignfile(f,'1234.dat');
  7.   if fileexists('1234.dat') then Append(F,1) //size of record be default is 128 you don't want that
  8.   else rewrite(f,1);
  9.   BlockWrite(f, vStr[1],length(vStr));
  10. end;
  11.  
and this only solves half of your problems you still do not know how long the string is when reading it back.
« Last Edit: August 02, 2017, 08:01:52 am by taazz »
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

questionstoaskislot

  • New Member
  • *
  • Posts: 16
  • I need to ask as much as questions I Can..
Re: how to write a .dat file part by part
« Reply #7 on: August 02, 2017, 08:17:43 am »
Ok dude ill try that

Handoko

  • Hero Member
  • *****
  • Posts: 5154
  • My goal: build my own game engine using Lazarus
Re: how to write a .dat file part by part
« Reply #8 on: August 02, 2017, 09:17:17 am »
I'm not sure what you're writing. Why you said there are 40 parts? Why not unlimited? I inspected your code, but unable to understand. So I wrote a demo showing how to create new, append, and show data using text file. You can download the test.zip

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Dialogs, StdCtrls, Spin,
  9.   ExtCtrls;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     btnNew: TButton;
  17.     btnRead: TButton;
  18.     btnNewSave: TButton;
  19.     btnNewCancel: TButton;
  20.     btnReadLine: TButton;
  21.     btnReadCancel: TButton;
  22.     Edit1: TEdit;
  23.     Label1: TLabel;
  24.     Notebook1: TNotebook;
  25.     Page1: TPage;
  26.     Page2: TPage;
  27.     SpinEdit1: TSpinEdit;
  28.     procedure FormCreate(Sender: TObject);
  29.     procedure btnNewClick(Sender: TObject);
  30.     procedure btnReadClick(Sender: TObject);
  31.     procedure btnNewSaveClick(Sender: TObject);
  32.     procedure btnNewCancelClick(Sender: TObject);
  33.     procedure btnReadCancelClick(Sender: TObject);
  34.     procedure btnReadLineClick(Sender: TObject);
  35.   private
  36.     procedure SaveData(Data: string);
  37.     procedure AppendData(Data: string);
  38.     function  GetTotal: integer;
  39.     function  ReadData(Line: Integer): string;
  40.   end;
  41.  
  42. var
  43.   Form1: TForm1;
  44.  
  45. implementation
  46.  
  47. {$R *.lfm}
  48.  
  49. const
  50.   FileName = 'MyData.dat';
  51.  
  52. { TForm1 }
  53.  
  54. procedure TForm1.FormCreate(Sender: TObject);
  55. begin
  56.   Notebook1.Visible := False;
  57. end;
  58.  
  59. procedure TForm1.btnNewClick(Sender: TObject);
  60. begin
  61.   btnNew.Enabled      := False;
  62.   btnRead.Enabled     := False;
  63.   Notebook1.Visible   := True;
  64.   Notebook1.PageIndex := 0;
  65.   Edit1.Text          := '';
  66. end;
  67.  
  68. procedure TForm1.btnReadClick(Sender: TObject);
  69. begin
  70.   btnNew.Enabled      := False;
  71.   btnRead.Enabled     := False;
  72.   Notebook1.Visible   := True;
  73.   Notebook1.PageIndex := 1;
  74.   SpinEdit1.MaxValue  := GetTotal;
  75. end;
  76.  
  77. procedure TForm1.btnNewSaveClick(Sender: TObject);
  78. var
  79.   Count: Integer;
  80. begin
  81.   if (Edit1.Text = '') then
  82.   begin
  83.     ShowMessage('Cannot save empty data.');
  84.     Exit;
  85.   end;
  86.   Count := GetTotal;
  87.   if (Count <= 0) then
  88.     SaveData(Edit1.Text)
  89.   else
  90.     AppendData(Edit1.Text);
  91.   Edit1.Text := '';
  92. end;
  93.  
  94. procedure TForm1.btnNewCancelClick(Sender: TObject);
  95. begin
  96.   Notebook1.Visible := False;
  97.   btnNew.Enabled    := True;
  98.   btnRead.Enabled   := True;
  99. end;
  100.  
  101. procedure TForm1.btnReadLineClick(Sender: TObject);
  102. var
  103.   Index: Integer;
  104.   S: string;
  105. begin
  106.   Index := SpinEdit1.Value;
  107.   S := ReadData(Index);
  108.   if (S <> '') then ShowMessage(S);
  109. end;
  110.  
  111. procedure TForm1.btnReadCancelClick(Sender: TObject);
  112. begin
  113.   Notebook1.Visible := False;
  114.   btnNew.Enabled    := True;
  115.   btnRead.Enabled   := True;
  116. end;
  117.  
  118. procedure TForm1.SaveData(Data: string);
  119. var
  120.   DataFile: TextFile;
  121. begin
  122.   AssignFile(DataFile, ProgramDirectory + FileName);
  123.   {$I+}
  124.   try
  125.     Rewrite(DataFile);
  126.     WriteLn(DataFile, Data);
  127.     CloseFile(DataFile);
  128.   except
  129.     ShowMessage('Error saving data.');
  130.   end;
  131. end;
  132.  
  133. procedure TForm1.AppendData(Data: string);
  134. var
  135.   DataFile: TextFile;
  136. begin
  137.   AssignFile(DataFile, ProgramDirectory + FileName);
  138.   try
  139.     Append(DataFile);
  140.     WriteLn(DataFile, Data);
  141.     CloseFile(DataFile);
  142.   except
  143.     ShowMessage('Error saving data.');
  144.   end;
  145. end;
  146.  
  147. function TForm1.GetTotal: integer;
  148. var
  149.   DataFile: TextFile;
  150.   Index: Integer;
  151.   S: string;
  152. begin
  153.  
  154.   Result := 0;
  155.  
  156.   AssignFile(DataFile, ProgramDirectory + FileName);
  157.   {$I-}
  158.   Reset(DataFile);
  159.   {$i+}
  160.   If (IOResult <> 0) then Exit;
  161.  
  162.   Index := 0;
  163.   while not EOF(DataFile) do
  164.   begin
  165.     Inc(Index);
  166.     ReadLn(DataFile, S);
  167.   end;
  168.   CloseFile(DataFile);
  169.   Result := Index;
  170.  
  171. end;
  172.  
  173. function TForm1.ReadData(Line: Integer): string;
  174. var
  175.   DataFile: TextFile;
  176.   Index: Integer; // start from 1
  177.   S: string;
  178. begin
  179.   Result := '';
  180.  
  181.   AssignFile(DataFile, ProgramDirectory + FileName);
  182.   try
  183.     Reset(DataFile);
  184.     Index := 0;
  185.     while not EOF(DataFile) do
  186.     begin
  187.       Inc(Index);
  188.       ReadLn(DataFile, S);
  189.       if (Index = Line) then Break;
  190.     end;
  191.     CloseFile(DataFile);
  192.     Result := S;
  193.   except
  194.     ShowMessage('Error reading data.'+#13+'Make sure it is not empty.');
  195.   end;
  196. end;
  197.  
  198. end.
« Last Edit: August 02, 2017, 09:20:02 am by Handoko »

questionstoaskislot

  • New Member
  • *
  • Posts: 16
  • I need to ask as much as questions I Can..
Re: how to write a .dat file part by part
« Reply #9 on: August 02, 2017, 09:41:27 am »
Thanks man i got it nobody understood that i was quiet stupid to understand something easily

Anyway thank you for every one

questionstoaskislot

  • New Member
  • *
  • Posts: 16
  • I need to ask as much as questions I Can..
Re: [SOLVED]how to write a .dat file part by part
« Reply #10 on: August 02, 2017, 06:15:12 pm »
Wow that helped a lot
Thanks so much Handoko
But there raised another problem .ill post the code with error when i can

 

TinyPortal © 2005-2018