Recent

Author Topic: [Solved] Trouble closiing a file  (Read 2349 times)

JFistere

  • New Member
  • *
  • Posts: 25
[Solved] Trouble closiing a file
« on: April 30, 2016, 08:20:51 am »
My program normally Resets a file in order to read it, and process the lines. However I occasionally need to edit the file, and I successfully open it using RunCommand statements and Notepad. However I found of course that I needed to close the read operation before I could successfully save anything. When I use the statement Close(TextFile), the compiler tells me I have used the wrong number of parameters for the Close operation. TextFile is type text and I have moved it around to make it more global, but it was already in Form1.

What am I doing wrong?
« Last Edit: April 30, 2016, 09:26:31 pm by JFistere »

Windsurfer

  • Sr. Member
  • ****
  • Posts: 368
    • Windsurfer
Re: Trouble closiing a file
« Reply #1 on: April 30, 2016, 09:10:58 am »
See http://www.freepascal.org/docs-html/rtl/system/close.html where there is an example. Make sure the file type is 'text', and try calling the file by a name other than 'textfile', say 'myfile'.

balazsszekely

  • Guest
Re: Trouble closiing a file
« Reply #2 on: April 30, 2016, 09:18:45 am »
Assign, Reset, Close are old pascal style function. Nowadays are much better ways to Load/Read text files(TStringList for example):
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   SL: TStringList;
  4.   I: Integer;
  5. begin
  6.   SL := TStringList.Create;
  7.   try
  8.     SL.LoadFromFile('Test.txt');
  9.     for I := 0 to SL.Count - 1 do
  10.     begin
  11.       //process each line
  12.       ShowMessage(SL.Strings[I]);
  13.     end;
  14.   finally
  15.     SL.Free;
  16.   end;
  17. end;

JFistere

  • New Member
  • *
  • Posts: 25
Re: Trouble closiing a file
« Reply #3 on: April 30, 2016, 09:25:19 pm »
@GETMem: Thanks for your answer. I got it working my existing way so I will save your method for the next program. If you care to, could you explain why your method is better than the old method?

@Windsurfer:  Thanks for your reference.

I did three things to solve the problem: 1) Change the filename to MyTextFile 2) Close the file in the same procedure I was opening it. I was opening it multiple times without ever closing it. 3) The thing that actually made the difference finally was to use System.Close(MyTextFile); rather that just Close. I found the last item in another forum.

 

TinyPortal © 2005-2018