?
Solved

problem with print out of textfiles

Posted on 1999-01-12
7
Medium Priority
?
481 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
[X]
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
  • 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
Independent Software Vendors: 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!

 
LVL 10

Accepted Solution

by:
viktornet earned 80 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

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…
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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month11 days, 20 hours left to enroll

752 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