Recent

Author Topic: Writing lines to text file?  (Read 591 times)

laznewb

  • New Member
  • *
  • Posts: 20
Writing lines to text file?
« on: February 29, 2020, 03:24:45 am »
Hi,

I'm trying to read fields in a SQLite database and write the fields to a text file.

This is the code:

Code: Pascal  [Select][+][-]
  1. while not Query.Eof do
  2. begin
  3.   StringA :=  'StringA: ' + Query.FieldByName('String_A').AsString + #13#10;
  4.   StringB := 'StringB: ' + Query.FieldByName('String_B').AsString + #13#10 + #13#10;
  5.  
  6.   AssignFile(f, 'C:\MyFolder\MyFile.txt');
  7.   Rewrite(f); // Here
  8.   Write(f, StringA + StringB);
  9.   Close(f);
  10.  
  11.   Query.Next;
  12. end;

At the moment, when I open the text file, only the last row in the database is written; this (I think) is because I'm rewriting the file each time it loops. But if I comment out that line, I get "raised exception class 'RunError(103)'".

What am I doing wrong here?
« Last Edit: February 29, 2020, 03:26:42 am by laznewb »

Scoops

  • Jr. Member
  • **
  • Posts: 86
Re: Writing lines to text file?
« Reply #1 on: February 29, 2020, 03:57:44 am »
AssignFile(f, 'C:\MyFolder\MyFile.txt');
Rewrite(f); // Here

need to moved up before the line 'while not Query.Eof do'

and

Close(f);

after the line 'end;'

everytime the code loops you are opening then closing the file, apart from your problem will also make things slooooooooow,  imagine reading 1000000 lines.......

basically
you need to open the file once
and close once.
not every loop.
« Last Edit: February 29, 2020, 04:22:40 am by Scoops »

dpremus

  • New Member
  • *
  • Posts: 23
Re: Writing lines to text file?
« Reply #2 on: February 29, 2020, 06:37:44 am »
As Scoops says you need to open and close file only once. For example, If your database table has
many records, your code will open and close file multiple times and this will slow down your code.

In your example, "Rewrite" command open and empty file in every iteration
of your loop, this is the reason why is only last record survived.

You can use WriteLn instead of appending CRLF (#13#10) at the end of the string.

Code: Pascal  [Select][+][-]
  1.  AssignFile(f, 'C:\MyFolder\MyFile.txt');
  2.  try
  3.    Rewrite(f);
  4.  
  5.    while not Query.eof do begin
  6.      WriteLn(f, 'StringA: ' + Query.FieldByName('String_A').AsString);
  7.      WriteLn(f, 'StringB: ' + Query.FieldByName('String_B').AsString);
  8.      WriteLn;
  9.      Query.Next;
  10.    end; // while ...
  11.  
  12.  finally
  13.    Close(f);
  14.  end;

ASerge

  • Hero Member
  • *****
  • Posts: 1615
Re: Writing lines to text file?
« Reply #3 on: February 29, 2020, 09:09:41 am »
Code: Pascal  [Select][+][-]
  1.  AssignFile(f, 'C:\MyFolder\MyFile.txt');
  2.  try
  3.    Rewrite(f);
  4. ...
Only a try after the Rewrite. AssignFile does not allocate OS resources. But the Rewrite makes it. After allocating resources, you can already "try".

Thaddy

  • Hero Member
  • *****
  • Posts: 10193
Re: Writing lines to text file?
« Reply #4 on: February 29, 2020, 09:31:09 am »
AssignFile is almost just an alias for Assign, so use that instead unless you need specific codepage support..
Code: Pascal  [Select][+][-]
  1. Assign(f, 'C:\MyFolder\MyFile.txt');
  2.  try
  3.    Rewrite(f);
AssignFile simply calls system.assign in most overloads
« Last Edit: February 29, 2020, 09:39:51 am by Thaddy »
I am more like donkey than shrek

MarkMLl

  • Hero Member
  • *****
  • Posts: 933
Re: Writing lines to text file?
« Reply #5 on: February 29, 2020, 10:03:34 am »
Also see the example at the bottom of https://www.freepascal.org/docs-html/current/rtl/system/ioresult.html which shows you how to use a compiler directive which allows you to check whether a file operation has been successful rather than having the program abend if e.g. a file isn't found.

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.

dpremus

  • New Member
  • *
  • Posts: 23
Re: Writing lines to text file?
« Reply #6 on: March 01, 2020, 01:36:18 pm »
Code: Pascal  [Select][+][-]
  1.  AssignFile(f, 'C:\MyFolder\MyFile.txt');
  2.  try
  3.    Rewrite(f);
  4. ...
Only a try after the Rewrite. AssignFile does not allocate OS resources. But the Rewrite makes it. After allocating resources, you can already "try".


Yeah, you are right, thanks for the fix.
If "rewrite" doesn't pass there is nothing to close.

 

TinyPortal © 2005-2018