Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

problem with print out of textfiles

Posted on 1999-01-12
7
Medium Priority
?
482 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
Technology Partners: 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

618 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