Recent

Author Topic: How to insert data from a text file on lazarus?  (Read 7452 times)

toyvoy

  • New Member
  • *
  • Posts: 11
How to insert data from a text file on lazarus?
« on: August 22, 2017, 09:45:56 am »
How to insert data from a text file on lazarus? What is the code of it?

Handoko

  • Hero Member
  • *****
  • Posts: 5154
  • My goal: build my own game engine using Lazarus
Re: How to insert data from a text file on lazarus?
« Reply #1 on: August 22, 2017, 10:10:41 am »
Not very sure what you want to do, but maybe you're interested to read:
http://forum.lazarus.freepascal.org/index.php/topic,37766.msg254800.html#msg254800

File handling and database tutorial for beginner:
http://wiki.freepascal.org/File_Handling_In_Pascal
http://wiki.freepascal.org/Lazarus_Tdbf_Tutorial

toyvoy

  • New Member
  • *
  • Posts: 11
Re: How to insert data from a text file on lazarus?
« Reply #2 on: August 22, 2017, 10:20:23 am »
I want to input some vocabulary to the program form a text file.
But the messages told me that it was wrong on the line( reset(f) )
I write my procedure as fellow.


procedure TForm1.Inputwords;
var f :textfile;
    n : integer;
begin
  n:=0;
   if Difficulty = 2 then begin
   assignfile(f,'Hard Mode.txt');
   reset(f);                                  <<<<<this line
   while eof(f)=false do begin
    n:=n+1;
    Readln(f,Word[2,n]);
   end;
   closefile(f);
  end;
end;                         

Handoko

  • Hero Member
  • *****
  • Posts: 5154
  • My goal: build my own game engine using Lazarus
Re: How to insert data from a text file on lazarus?
« Reply #3 on: August 22, 2017, 10:27:12 am »
What error did it say? Have you make sure the file exists on the correct directory?

toyvoy

  • New Member
  • *
  • Posts: 11
Re: How to insert data from a text file on lazarus?
« Reply #4 on: August 22, 2017, 10:33:25 am »
The Messages:
Error: Wrong number of parameters specified for call to "reset"
Error: Found declaration: reset;
The file is in the same file where the program is.
« Last Edit: August 22, 2017, 10:35:20 am by toyvoy »

Handoko

  • Hero Member
  • *****
  • Posts: 5154
  • My goal: build my own game engine using Lazarus
Re: How to insert data from a text file on lazarus?
« Reply #5 on: August 22, 2017, 10:39:01 am »
Can you please provide the whole source code and all the necessary files? Copy them to a new folder, except: *.exe, *.bak and lib folder. Compress the folder and send the zip file to this forum.

toyvoy

  • New Member
  • *
  • Posts: 11
Re: How to insert data from a text file on lazarus?
« Reply #6 on: August 22, 2017, 10:56:39 am »
thanks for helping :)

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: How to insert data from a text file on lazarus?
« Reply #7 on: August 22, 2017, 11:08:23 am »
So what does the Form1.Reset; function do??

When you call reset(f); in TForm1.Inputwords; it wants to call TForm1.Reset; and NOT the reset(f:file) for file-functions.

Rename the Form1.Reset to something else (like ResetGame) and that error-message goes away.

toyvoy

  • New Member
  • *
  • Posts: 11
Re: How to insert data from a text file on lazarus?
« Reply #8 on: August 22, 2017, 11:12:17 am »
So what does the Form1.Reset; function do??

When you call reset(f); in TForm1.Inputwords; it wants to call TForm1.Reset; and NOT the reset(f:file) for file-functions.

Rename the Form1.Reset to something else (like ResetGame) and that error-message goes away.

sorry I'm so careless and thanks

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: How to insert data from a text file on lazarus?
« Reply #9 on: August 22, 2017, 11:35:23 am »
There was a British propaganda campaign in the 1940s called "Careless talk costs lives"...

Carelessness in programming leads to all kinds of bugs.
Careful naming can help avoid some of them.
Thus your global array word[] would be better named slightly differently, to avoid any confusion with the Word type denoting an unsigned integer.

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: How to insert data from a text file on lazarus?
« Reply #10 on: August 22, 2017, 01:36:50 pm »
Off/on topic: https://en.wikipedia.org/wiki/2003_attack_on_Karbala shows how carelessness can lead to casualties. (triggered by howardpc, dunno why I made the connection, just came to mind)..
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

ASerge

  • Hero Member
  • *****
  • Posts: 2242
Re: How to insert data from a text file on lazarus?
« Reply #11 on: August 22, 2017, 04:26:03 pm »
To @toyvoy.
A few comments on the source code.

1. Get rid of global variables, this is generally a "bad" style. At least move them to the private section of the TForm1. If you later want to create a second instance of the form, you do not have to double the global variables.

2. The word is not a very suitable name for variable. It hides the built-in type word. So if you use the note above and add the letter F to the field name (common practice), you get FWord (maybe even better name FWords).

3. In FormPaint the try finally construct is used incorrectly (for some reason, the popular error). You must protect the resource after its allocation. Those you need to do
Code: Pascal  [Select][+][-]
  1. Instance := TSomeClass.Create;
  2. try
  3.   //Code with Instance
  4. finally
  5.   Instance.Free;
  6. end;
i.e. not transfer the initialization after a try. In your case, for example, if an exception occurs during the execution of the constructor, then call of Instance.Free will, at best case, cause a nested exception, but if in the Instance (garbage on the stack) will be the address of the real object (the main form, for example :) ) then you spent a lot of time to find an error in the program.

4. The InputWords procedure. If you protect a memory leak when creating an object, then the system resource (file) is all the more in need of protection, i.e. use try finally.
Code: Pascal  [Select][+][-]
  1. AssignFile(f, ...);
  2. Reset(f);
  3. try
  4.   // Use file
  5. finally
  6.   CloseFile(f);
  7. end;

5. Expression if SomeBool = False then (or if SomeBool = True then) look clumsy. It is better to use the natural if not SomeBool then (or if SomeBool then).

6. VisibleAll and InvisibleAll procedures better be merged into one like SetVisible(AVisible: Boolean), then inside you use Field.Visible: = AVisible (or := not AVisible), and call as SetVisible(True) or SetVisible(False).

7. For the case statement, it's better to place the operators below line of constant value, so when debugging, you'll see which case was selected.

And returning to the original question: with name conflicts, you can specify the required procedure using the module name, that is, specify System.Reset.

RAW

  • Hero Member
  • *****
  • Posts: 868
Re: How to insert data from a text file on lazarus?
« Reply #12 on: August 22, 2017, 06:37:49 pm »
 :D

Code: Pascal  [Select][+][-]
  1. Function LoadTextFile(strFile: String): String;
  2.   Var
  3.    fs    : TFileStream;
  4.    str   : String;
  5.    iBytes: Integer;
  6.  Begin
  7.   Result:= '';
  8.    If Not FileExists(strFile) Then Exit;
  9.  
  10.   fs:= TFileStream.Create(strFile, fmOpenRead);
  11.    Try
  12.     SetLength(str, fs.Size);
  13.     iBytes:= fs.Read(str[1], fs.Size);
  14.  
  15.     If iBytes = fs.Size Then Result:= str;
  16.    Finally
  17.     fs.Free;
  18.    End;
  19.  End;
  20.  
  21. Procedure TForm1.FormClick(Sender: TObject);
  22.  Begin
  23.   Memo1.Text:= LoadTextFile('I:\Temp\W7\Hard words.txt');
  24.  End;

Code: Pascal  [Select][+][-]
  1. Procedure TForm1.FormClick(Sender: TObject);
  2.   Var
  3.    str: String;
  4.  Begin
  5.   str:= LoadTextFile('I:\Temp\W7\Hard words.txt');
  6.  
  7.   If str <> '' Then ShowMessage(str);
  8.  End;
« Last Edit: August 22, 2017, 06:42:48 pm by RAW »
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

RAW

  • Hero Member
  • *****
  • Posts: 868
Re: How to insert data from a text file on lazarus?
« Reply #13 on: August 22, 2017, 06:49:51 pm »
OR....

Code: Pascal  [Select][+][-]
  1. Function LoadTextFile(strFile: String; Out strOut: String): Boolean;
  2.   Var
  3.    fs    : TFileStream;
  4.    str   : String;
  5.    iBytes: Integer;
  6.  Begin
  7.   Result:= False;
  8.    If Not FileExists(strFile) Then Exit;
  9.  
  10.   fs:= TFileStream.Create(strFile, fmOpenRead);
  11.    Try
  12.     SetLength(str, fs.Size);
  13.     iBytes:= fs.Read(str[1], fs.Size);
  14.  
  15.     If iBytes = fs.Size
  16.     Then
  17.      Begin
  18.       strOut:= str;
  19.       Result:= True;
  20.      End;
  21.    Finally
  22.     fs.Free;
  23.    End;
  24.  End;
  25.  
  26. Procedure TForm1.FormClick(Sender: TObject);
  27.   Var
  28.    str: String;
  29.  Begin
  30.   If LoadTextFile('I:\Temp\W7\Hard words.txt', str)
  31.   Then ShowMessage(str);
  32.  End;
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

toyvoy

  • New Member
  • *
  • Posts: 11
Re: How to insert data from a text file on lazarus?
« Reply #14 on: August 23, 2017, 11:58:14 am »
3. In FormPaint the try finally construct is used incorrectly (for some reason, the popular error). You must protect the resource after its allocation. Those you need to do
Code: Pascal  [Select][+][-]
  1. Instance := TSomeClass.Create;
  2. try
  3.   //Code with Instance
  4. finally
  5.   Instance.Free;
  6. end;
i.e. not transfer the initialization after a try. In your case, for example, if an exception occurs during the execution of the constructor, then call of Instance.Free will, at best case, cause a nested exception, but if in the Instance (garbage on the stack) will be the address of the real object (the main form, for example :) ) then you spent a lot of time to find an error in the program.

Actually, I want to change the background image using FormPaint, but it falls obviously. Could you please explain more about it?
I don't know what this means :Instance := TSomeClass.Create;
and in this line [//Code with Instance]
what should I write in order to change the form's background with a jpg picture?
Thanks a lot for helping again! :)
« Last Edit: August 23, 2017, 12:00:08 pm by toyvoy »

 

TinyPortal © 2005-2018