Board index » delphi » problems with program

problems with program

i am creating a program to conduct a binary search on a file with the name
of a town, and then give the user the distance of that town from london. it
is part of a school assignment.

problems i have are....

part of my program asks the user to input a file they wish to conduct a
search on. of course this would be copied into an array in memory first, but
the user must specify the file.
how can i check to see if a file is in existance before i assign it in my
program, as if i assign it, and the file they entered does not actually
exist, then i will get a file not found error, and my program will
terminate. much better if i can have an error handling algorithm present to
deal with these things.

when the user inputs the file, i would like it in the format :

                                             filename.cdh

is there a way to tell if the user has entered "filename.cdh" or just
"filename", and if the user has entered just the filename, then i would like
the program to attach the ".cdh" to the string before it assigns the file.

var filename : string
      file1 : file of array; {array not relevant here, i dont think}

begin
   readln(flename);
   assign(file1,filename);
end;

if the file does not exist - crash
if they type in no extension, then it will also crash.

any ideas?

thanx

craig

 

Re:problems with program


Quote
> i am creating a program to conduct a binary search on a file with the name
> of a town, and then give the user the distance of that town from london. it
> is part of a school assignment. problems i have are....
> part of my program asks the user to input a file they wish to conduct a
> search on. of course this would be copied into an array in memory first, but
> the user must specify the file.
> how can i check to see if a file is in existance before i assign it in my
> program, as if i assign it, and the file they entered does not actually
> exist, then i will get a file not found error, and my program will
> terminate. much better if i can have an error handling algorithm present to
> deal with these things.

   Here is an "exists" function you can use:
function Exists (FN : string) : boolean;     { test for file existence }
var F : SearchRec;
begin
  FindFirst (FN,AnyFile,F); Exists := DosError = 0
end;  { Exists }
   Since you're probably unfamiliar with FindFirst, DOSError, and
SearchRec, I strongly suggest that you look them up and determine what's
going on here.  After all, you _do_ want to be able to defend/explain
your code when your instructor queries you about it, don't you?

Quote
> when the user inputs the file, i would like it in the format :
>                                              filename.cdh
> is there a way to tell if the user has entered "filename.cdh" or just
> "filename", and if the user has entered just the filename, then i would like
> the program to attach the ".cdh" to the string before it assigns the file.

   Yes: FSplit

Quote
> var filename : string
>       file1 : file of array; {array not relevant here, i dont think}

   It's also not legal Pascal syntax here.  It would have been more
helpful if you had posted your data description, so we could offer help
on other things you have wrong...

Quote
> begin
>    readln(flename);

    if Exist(filename) then
      begin
Quote
>        assign(file1,filename); Reset (File1);

      { read data and load array }
        Close (File1);
      { process your query/search }
Quote
>     end

    else {display error message and terminate }
Quote

> if the file does not exist - crash
> if they type in no extension, then it will also crash.

   "Crash"?  Poor terminology for any program _you_ write; you should
always be thinking of _graceful_ exits with descriptive error messages,
as well as complete displays of information when/if the program
terminates normally.  The user should _ALWAYS_ know what happened when
the program ran, as well as sufficient information regarding what to do
if it failed or didn't run correctly.  That's _your_ job as a
designer/programmer, not the user's to figure out from some arcane "104
error" or such when the program actually _does_ "crash".

Re:problems with program


JRS:  In article <8dkrg7$ke...@gxsn.com> of Tue, 18 Apr 2000 11:36:04
seen in news:comp.lang.pascal.borland, Craig Hennessey <cdhennessey@free

Quote
netname.co.uk> wrote:
>part of my program asks the user to input a file they wish to conduct a
>search on. of course this would be copied into an array in memory first, but
>the user must specify the file.
>how can i check to see if a file is in existance before i assign it in my
>program, as if i assign it, and the file they entered does not actually
>exist, then i will get a file not found error, and my program will
>terminate. much better if i can have an error handling algorithm present to
>deal with these things.

See TSFAQP #47.  Or my WWW site.

Quote
>when the user inputs the file, i would like it in the format :

>                                             filename.cdh

>is there a way to tell if the user has entered "filename.cdh" or just
>"filename", and if the user has entered just the filename, then i would like
>the program to attach the ".cdh" to the string before it assigns the file.

Yes.  See Pos in Help.

Please write in proper English, with capital letters where required.  It
is a courtesy towards foreigners who want to improve their English, and
to those of us who are accustomed to properly-written material.  The
practice will do you good, too; few employers would consider anyone who
writes like that to be employable.

--
? John Stockton, Surrey, UK.  j...@merlyn.demon.co.uk   Turnpike v4.00   MIME. ?
 <URL: http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
 <URL: ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ;
 <URL: http://www.merlyn.demon.co.uk/clpb-faq.txt> Pedt Scragg: c.l.p.b. mFAQ.

Re:problems with program


JRS:  In article <MPG.1367e650e7b88942989...@news.primenet.com> of Wed,
19 Apr 2000 15:51:16 seen in news:comp.lang.pascal.borland, Mike

Quote
Copeland <mrc...@primenet.com> wrote:
>   Here is an "exists" function you can use:
>function Exists (FN : string) : boolean;     { test for file existence }
>var F : SearchRec;
>begin
>  FindFirst (FN,AnyFile,F); Exists := DosError = 0
>end;  { Exists }

Alas, that is an "exists" function, but not a "file exists" one.  On
most PCs nowadays, it will return TRUE if FN = '\windows'.  It is a
"Name exists" function.  It will also return TRUE for FN = '*.*', or FN
= '\<Vol-ID>'.  Also for FN = 'NUL', a case in which one may want either
answer ...

There are some existence routines in my Web programs/chk_xist.pas ; as
well as TSFAQP #47, #45 is also relevant ; my pas-norm.htm#Fxist refers.

I agree wholeheartedly, though, with the approach of getting the
Attribute byte and checking it carefully; I consider that definitive,
for a DOS file.

I've just noticed that, according to FindFirst, "files" NUL, CON, AUX
have the "$40" bit set.

--
? John Stockton, Surrey, UK.  j...@merlyn.demon.co.uk   Turnpike v4.00   MIME. ?
 Web <URL: http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.

Food expiry ambiguities: <URL: http://www.merlyn.demon.co.uk/date2k-3.htm#Food>

Re:problems with program


Craig Hennessey <cdhennes...@freenetname.co.uk> p1e v diskusnm
p?spvku:8dkrg7$ke...@gxsn.com...

Quote
> i am creating a program to conduct a binary search on a file with the name
> of a town, and then give the user the distance of that town from london.
it
> is part of a school assignment.

> problems i have are....

> part of my program asks the user to input a file they wish to conduct a
> search on. of course this would be copied into an array in memory first,
but
> the user must specify the file.
> how can i check to see if a file is in existance before i assign it in my
> program, as if i assign it, and the file they entered does not actually
> exist, then i will get a file not found error, and my program will
> terminate. much better if i can have an error handling algorithm present
to
> deal with these things.

> when the user inputs the file, i would like it in the format :

>                                              filename.cdh

> is there a way to tell if the user has entered "filename.cdh" or just
> "filename", and if the user has entered just the filename, then i would
like
> the program to attach the ".cdh" to the string before it assigns the file.

> var filename : string
>       file1 : file of array; {array not relevant here, i dont think}

> begin
>    readln(flename);
>    assign(file1,filename);
> end;

> if the file does not exist - crash
> if they type in no extension, then it will also crash.

> any ideas?

> thanx

> craig

I've read solutions that other people gave you but I think that the best
solution is this:

var s:file; {of any type}
       x:string;
       er:integer;

begin
   x:='c:\file.dat';
   assign (s,x);
   {$I-}     <- this directive lets you to control input-output errors; when
there's an error, no error sub-routine will be called
  reset (s);
er:=ioresult;  <- input/output result
if er<>0 then begin
    Writeln ('Could not handle with ',x);
    halt;
end;
 {$I+} <-- this directive lets program to control IO errors
end.

If the file will be hidden, findfirst will find it and there will be an
error. But when you use ioresult, it will work well.

md.

[ mand...@volny.cz  http://darkarea.hypermart.net ]

Other Threads