• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 483
  • Last Modified:

problem with print out of textfiles

Hi experts,

with the code at the end of this mail I try to print several textfiles
which I selected from a filelistbox.

Unfortunately, the line

 while not eof(filename) do begin

causes a run-time error:

exception of class ElnOutError; I/O error 104.

Here is my source code:

unit print;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, FileCtrl, printers;

type
  TForm1 = class(TForm)
    Button1: TButton;
    PrintDialog1: TPrintDialog;
    OpenDialog1: TOpenDialog;
    FileListBox1: TFileListBox;
    DirectoryListBox1: TDirectoryListBox;
    DriveComboBox1: TDriveComboBox;
    FilterComboBox1: TFilterComboBox;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
  i: integer;
  dir: string;
  lines: string;
  printer, filename: textfile;
begin
  dir := directorylistbox1.directory;
  if dir[length(dir)] <> '\' then dir := dir + '\';
  if messagebox(0, 'Print all selected files?', 'Question', 36) = 6 then begin
    for i := 0 to filelistbox1.items.count - 1 do begin
      if filelistbox1.selected[i] then begin
        try
          assignPrn(printer);
          rewrite(printer);
          reset(filename, lines);
          while not eof(filename) do begin  {this line causes a run-time error}
            readln(filename, lines);
            writeln(printer, lines);
          end;
        finally
          closefile(filename);
          closefile(printer);
        end; {finally}
      end; {if}
    end; {for}
  end;{if}
end; {procedure}

end.




Can you please tell me, what I am doing wrong here ?

With kind regards

Mathes

 
0
mathes
Asked:
mathes
  • 3
  • 2
  • 2
1 Solution
 
sakyaCommented:
Hi  Mathes

This is how to open a textfile.

1.var filename: textFile;
2.AssignFile(filename, testfile_name_you_want_to_open);
3.If you want to open a file with read-only,
write Reset(filename) method;

If change your code,new codes is as follows.

AssignFile(filename, dir + filelistbox1.Items[i]);
Reset(filename);
while not eof(filename) do
begin
  readln(filename, lines);
  writeln(printer, lines);
end;

Regards

sakya
0
 
viktornetCommented:
The new code should look like this,,,

procedure TForm1.Button1Click(Sender: TObject);
   var
     i: integer;
     dir: string;
     lines: string;
     printer, filename: textfile;
   begin
     dir := directorylistbox1.directory;
     if dir[length(dir)] <> '\' then dir := dir + '\';
     if messagebox(0, 'Print all selected files?', 'Question', 36) = 6 then begin
       for i := 0 to filelistbox1.items.count - 1 do begin
         if filelistbox1.selected[i] then begin
           try
             AssignPrn(printer);
             Rewrite(printer);
             AssignFile(filename, dir + FileListBox.Items[i]);
             Reset(filename);
             while not eof(filename) do begin  {this line causes a run-time error}
               readln(filename, lines);
               writeln(printer, lines);
             end;
           finally
             closefile(filename);
             closefile(printer);
           end; {finally}
         end; {if}
       end; {for}
     end;{if}
   end; {procedure}

   end.

Hope this help....

-Viktor
--Ivanov
0
 
mathesAuthor Commented:

Hi experts,

thank you for your proposals.

Sakya: Is it really correct not to use the "assignPrn" statement? To my knowledge, this statement is
required in order to establish a connection between the printer and my textfiles.

Victornet:  Your code works excellent. I suggest you change your comment to an answer, and I will give you the rating points.

With kind regards

mathes
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
viktornetCommented:
Thanks...

AssignPrn() is used to assign a file to the printer.. Now when you write any text to that file it will be output to the printer... so you can use AssignPrn() and is a good choice to do so...

Thanks for the credit!

-Viktor
--Ivanov
0
 
viktornetCommented:
Just to tell you what was wrong with the way you tried to do it...

When you declare a variable of type TextFile or even File it doesn't assign a file name by itself. That's way you should use AssignFile(File, FileName); to assign a filename to the file that you created.. then when you use reset(file); it will know which file to open for reading...

If you need more info let me know...

-Viktor
--Ivanov
0
 
sakyaCommented:
Hi mathes

I just changed a wrong part of your codes.
I want you to write 'AssignFile' before 'Reset',
and chang a parameter of  the Reset statement.

I'm sorry that my mail leads misunderstanding.
I'm Japanese, so English is very handicapped.

I think that viktor's comment is the same as my answer.
Anyway I don't want points ,I just help you.

Regards.

sakya
 
 
0
 
mathesAuthor Commented:
Hi experts,

thank you for your answers.

viktornet: Thank you for providing me with further informnation why my code did not work.
I think it is very important to und4rstand why an eror did happen and not only to correct the error.
By doing so, I will avoid that this error won't happen again in the future.

Sakya: Yes, after viewing your second reply I think you had the same idea as victornet.
As victornet was the first who provided me with a correct answer, I will give him the points.

Sometimes it is very difficult to be fair. Perhaps the webmaster of this site should consider to
implement the possibility to distribute the available points to several experts, if the help seeking
programmer recieved more than just one reasonable answer.

with kind regards

mathes


0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now