Solved

Help needed, easy question for experts!

Posted on 2003-11-23
6
296 Views
Last Modified: 2011-08-18
I have this program so far - i need to return an error message if the file entered doesnt exist....

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var Input      : text;
      I          : integer;
      Lineoftext : string;
      S : string;

  Begin

  Readln (S);

  Reset(input,S);
  for I:=1 to 10 do begin
  Readln (Input,Lineoftext); {Reading a Lineoftext from input file}
  writeln (Lineoftext);
  end;
  Closefile (Input);
  readln;
  end.


Thanks :)

0
Comment
Question by:jarhys
6 Comments
 
LVL 9

Accepted Solution

by:
ginsonic earned 25 total points
ID: 9805867
Just use FileExists function as:

if not FileExists( S )  then begin
  ShowMessage( 'The file don't exist!' );
  Exit;
end;
0
 
LVL 8

Assisted Solution

by:gmayo
gmayo earned 25 total points
ID: 9805871
if FileExists(LineOfText) then begin
   ...file exists...
   ExitCode := 0;
end else begin
   ... file does not exist...
   ExitCode := 1; // or whatever
end;

It's in the SysUtils unit which you already include.

Geoff M.
0
 
LVL 4

Expert Comment

by:nestorua
ID: 9805931
HI,
You also can use "try..except..end" to catch the error.
Sincerely,
Nestorua.
0
 
LVL 5

Expert Comment

by:DeerBear
ID: 9810056
Hi,

Eventually you can also use FileStream, which would also allow for more
flexybility in your data processing.

HTH,

Andrew
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

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 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…

791 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