Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

problem with print out of textfiles

Posted on 1999-01-12
7
478 Views
Last Modified: 2010-04-06
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
Comment
Question by:mathes
  • 3
  • 2
  • 2
7 Comments
 

Expert Comment

by:sakya
ID: 1356126
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
 
LVL 10

Expert Comment

by:viktornet
ID: 1356127
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
 

Author Comment

by:mathes
ID: 1356128

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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 10

Accepted Solution

by:
viktornet earned 20 total points
ID: 1356129
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
 
LVL 10

Expert Comment

by:viktornet
ID: 1356130
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
 

Expert Comment

by:sakya
ID: 1356131
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
 

Author Comment

by:mathes
ID: 1356132
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question