File handling. 
Author Message
 File handling.

I've been writing this ciphering/deciphering program in TP7.
There is some small problems though.

1. I'm using the ascii code for each character and then I'm adding the ascii
code for the corresponding character in the key.
This is not the most secure way I know but I'm not a very good programmer.

2. When the user enters a filename thats doesn't exist, a runtime error
occures.
Is there a good way to tell the user that the file doesn't exist and make
him/her reenter the filename?

Martin Isaksson



Wed, 18 Jun 1902 08:00:00 GMT  
 File handling.
Hi!

Quote:
> 2. When the user enters a filename thats doesn't exist, a runtime error
> occures.
> Is there a good way to tell the user that the file doesn't exist and make
> him/her reenter the filename?

You could use IoResult to proove if the file can be accessed.

CU
Jens

--
[ MY WEBPAGES (IN GERMAN AND ENGLISH) ]

[ HOMEPAGE: http://home.t-online.de/home/jens.gesing/index.htm ]
[ PROGRAMMING-DO-IT-YOURSELF: http://members.xoom.com/goldman47/index.htm ]



Wed, 18 Jun 1902 08:00:00 GMT  
 File handling.

Quote:

> I've been writing this ciphering/deciphering program in TP7.
> There is some small problems though.

> 1. I'm using the ascii code for each character and then I'm adding the ascii
> code for the corresponding character in the key.
> This is not the most secure way I know but I'm not a very good programmer.

> 2. When the user enters a filename thats doesn't exist, a runtime error
> occures.
> Is there a good way to tell the user that the file doesn't exist and make
> him/her reenter the filename?

Oh yes, it is!

  Assign(T,FilNam);
{$I-}
  Reset(T);
{$I+]
  if IOresult = 0 then
    Begin
      ReadLn .......
      Close(T);
    End
  else
    Begin
      Writeln('Error: Could not open the file ',FilNam);
    End;

Actually: it is much better to put this into a function, eg.:

Function OpenTextFile(Var T : Text; FilNam:String):Boolean;
....
End;

if OpenTextFile(....) then...

:-)))
--
Franz Glaser, Glasau 3, A-4191 Vorderweissenbach Austria +43-7219-7035-0
Muehlviertler Elektronik Glaser.  Industrial control and instrumentation
http://members.eunet.at/meg-glaser/    http://members.xoom.com/f_glaser/
http://www.geocities.com/~franzglaser/            http://start.at/bedarf



Wed, 18 Jun 1902 08:00:00 GMT  
 File handling.

: I've been writing this ciphering/deciphering program in TP7.
: There is some small problems though.
:
: 1. I'm using the ascii code for each character and then I'm adding the
ascii
: code for the corresponding character in the key.
: This is not the most secure way I know but I'm not a very good programmer.

Remember to use Inc() and Dec() to make sure you always get the right
results even if the results exceeds 255 or 0.

: 2. When the user enters a filename thats doesn't exist, a runtime error
: occures.
: Is there a good way to tell the user that the file doesn't exist and make
: him/her reenter the filename?

This function is a cute one:

function FileExists(AFileName : string): Boolean;
var Info : SearchRec;
begin
   FindFirst(AFilename, AnyFile, DirInfo);
   FileExists := DosError = 0;
end;

repeat until FileExists(UsersInput);



Wed, 18 Jun 1902 08:00:00 GMT  
 File handling.


Quote:

>This function is a cute one:

>function FileExists(AFileName : string): Boolean;
>var Info : SearchRec;                          <----- DirInfo
>begin
>   FindFirst(AFilename, AnyFile, DirInfo);
>   FileExists := DosError = 0;
>end;

ISTM that your function will return TRUE for a VolumeID or Directory;
and, if running in a Windows DOS box, perhaps also for a file which is
in use and locked by another application to be inaccessible.

See TSFAQP #47.

In <URL: http://www.merlyn.demon.co.uk/programs/>, read chk-xist.pas.

--

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



Wed, 18 Jun 1902 08:00:00 GMT  
 File handling.



: >function FileExists(AFileName : string): Boolean;
: >var Info : SearchRec;                          <----- DirInfo
: >begin
: >   FindFirst(AFilename, AnyFile, DirInfo);
: >   FileExists := DosError = 0;
: >end;
:
: ISTM that your function will return TRUE for a VolumeID or Directory;
: and, if running in a Windows DOS box, perhaps also for a file which is
: in use and locked by another application to be inaccessible.
:
: See TSFAQP #47.
:
: In <URL: http://www.merlyn.demon.co.uk/programs/>, read chk-xist.pas.

Ups, guess I have to slow down on my writing. Couple of bugs in my answer
there.
I ment to write: FindFirst(AFilename, AnyFile - Directory, Info) to get the
files only,
and, yes, TMIS to return TRUE if the file is there. I don't know what
Isaksson wanted to do with it.
This function was just ment to be simple, but I have read your
"chk-xist.pas" and I fully agree that there exists much better solutions
than mine due to all the precautions to make.
BTW: Your Website is by now added to my Favourites. Impressive. Thank you.
:-)
-ThomasN



Wed, 18 Jun 1902 08:00:00 GMT  
 File handling.
Whenever I'm writing a program with text file access, I put in the
following 2 functions:

function OpenFileRead(var f:text;name:string):boolean;
begin
     Assign(f,name);     {or is it the other way round?}
     {$I-}
     Reset(f);
     {$I+}
     OpenFileRead:=IOResult=0;
end;

Likewise:

function OpenFileWrite(var f:text;name:string):boolean;
begin
     Assign(f,name);     {or is it the other way round?}
     {$I-}
     ReWrite(f);
     {$I+}
     OpenFileWrite:=IOResult=0;
end;

With these, calling the function will open the file if it can, and if
not, return False. Eg:

if OpenFileRead(myfile,'c:\whatever.txt')=false then begin
     writeln;
     writeln('Error : cannot read from file c:\whatever.txt');
     Halt(1);
     end
else begin
     { do whatever you want to do with the file and close it }

Similar to the previous reply, but more general being a function.



Quote:


> > I've been writing this ciphering/deciphering program in TP7.
> > There is some small problems though.

> > 1. I'm using the ascii code for each character and then I'm adding
the ascii
> > code for the corresponding character in the key.
> > This is not the most secure way I know but I'm not a very good
programmer.

> > 2. When the user enters a filename thats doesn't exist, a runtime
error
> > occures.
> > Is there a good way to tell the user that the file doesn't exist and
make
> > him/her reenter the filename?

> Oh yes, it is!

>   Assign(T,FilNam);
> {$I-}
>   Reset(T);
> {$I+]
>   if IOresult = 0 then
>     Begin
>       ReadLn .......
>       Close(T);
>     End
>   else
>     Begin
>       Writeln('Error: Could not open the file ',FilNam);
>     End;

> Actually: it is much better to put this into a function, eg.:

> Function OpenTextFile(Var T : Text; FilNam:String):Boolean;
> ....
> End;

> if OpenTextFile(....) then...

> :-)))
> --
> Franz Glaser, Glasau 3, A-4191 Vorderweissenbach Austria
+43-7219-7035-0
> Muehlviertler Elektronik Glaser.  Industrial control and
instrumentation
> http://members.eunet.at/meg-glaser/

http://members.xoom.com/f_glaser/
Quote:
> http://www.geocities.com/~franzglaser/

http://start.at/bedarf

--
+---------------+---------------------------+

|               | (Remove the NOSPAM.)      |
+---------------+---------------------------+

Sent via Deja.com http://www.deja.com/
Before you buy.



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Losing File Handles - But I am not using Files

2. Creating executable file + file handling "objects"

3. BDE Error: # of Max File handles exceeded

4. HELP: File handling and encryption problems!

5. XOR encryption program file handling

6. XOR encryption program file handling

7. Table.Close does not close file handle?

8. HELP: File handling and encryption problems!

9. File handling

10. need help on file handling

11. record file handling problem

12. Wierd - database file handle limits - Help!!!!

 

 
Powered by phpBB® Forum Software