Solved

problem with print out of textfiles

Posted on 1999-01-12
7
474 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

747 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now