Board index » delphi » File Handling in Turbo Pascal

File Handling in Turbo Pascal

I'm really sorry but does anyone know where I can find information on
TurboPascal file handling? I'm trying to write a program that allows the
user to enter the path and file name of a text file and then manipulate
that file. The problem is that in school we only learned Standard
Pascal. So is there some way to allow the user to enter the path and
file themselves? Thanks so much.

Lilly Irani
lir...@ix.netcom.com
--
****************************************************
*Era un da muy sombro. Ella necesitaba un nombre.*
* El nabo sangriento se llam "el cacto borroso" y *
*  la luz se apareci. Y, como esto, Spike naci.  *
* [credit for idea: The {*word*76}y Turnip, canonized   *
*           saint in Lillican Church]              *
****************************************************
*        http://www.netsurge.com/trillian/         *
****************************************************

 

Re:File Handling in Turbo Pascal


On Wed, 18 Jun 1997 00:31:41 -0700, Trillian <lir...@ix.netcom.com>
wrote:

Quote
> So is there some way to allow the user to enter the path and
>file themselves?

Var FileId : String;

Readln (FileId);

Use help to find Assign, Reset and Rewrite.

If I have misunderstood your question please post it differently.

Dennis D. Powers
PC/POLL SYSTEMS
Den...@pcpoll.com

Support the anti-Spam amendment
Join at http://www.cauce.org/

Re:File Handling in Turbo Pascal


On Wed, 18 Jun 1997 00:31:41 -0700, Trillian <lir...@ix.netcom.com>
wrote:

Quote
>I'm really sorry but does anyone know where I can find information on
>TurboPascal file handling? I'm trying to write a program that allows the
>user to enter the path and file name of a text file and then manipulate
>that file. The problem is that in school we only learned Standard
>Pascal. So is there some way to allow the user to enter the path and
>file themselves? Thanks so much.
>Lilly Irani

No need to be sorry, that's what the list is for.

Turbo Pascal provides a built-in "string" type that makes this kind of
work MUCH simpler than in Standard Pascal; here's a very simple
(especially, no error checking!) program that does basically what
you're describing:

-------------------------->8 cut here 8<--------------------------
program GetFile ;
var
  FileName : string ;
  TheFile : Text ;
begin
  WriteLn( output, 'File to process:' ) ; (* prompt *)

  ReadLn( input, FileName ) ;  (* get name from user *)

  Assign( TheFile, FileName ) ; (* associate external filename with  
    internal file variable *)

  Reset( TheFile ) ;  (* prepare file for reading *)
  (* alternatively use ReWrite() if you want to write to the file *)
  (* or Append() if you want to add to an already existing file   *)

  (* do your file processing here, be sure to prepend your  *)
  (* Write[Ln]s or Read[Ln]s with the file variable name    *)
  (*   e.g. WriteLn( TheFile, 'This text goes in the file') *)

  Close( TheFile ) ;  (* flush any buffers, write file to disk *)
end .
-------------------------->8 cut here 8<--------------------------

Email me if this isn't clear or you need more info.

Quote
>lir...@ix.netcom.com
>--
>****************************************************
>*Era un da muy sombro. Ella necesitaba un nombre.*
>* El nabo sangriento se llam "el cacto borroso" y *
>*  la luz se apareci. Y, como esto, Spike naci.  *
>* [credit for idea: The {*word*76}y Turnip, canonized   *
>*           saint in Lillican Church]              *
>****************************************************
>*        http://www.netsurge.com/trillian/         *
>****************************************************

What little my college Spanish classes will let me glean from this
makes it sound VERY interesting, could you provide a more thorough
translation?

Regards,

Stephen Posey
slpo...@concentric.net

Beware of Geeks bearing GIFs!

Re:File Handling in Turbo Pascal


Trillian <lir...@ix.netcom.com> wrote in article
<33A78EDD.7...@ix.netcom.com>...

Quote
> I'm really sorry but does anyone know where I can find information on
> TurboPascal file handling? I'm trying to write a program that allows the
> user to enter the path and file name of a text file and then manipulate
> that file. The problem is that in school we only learned Standard
> Pascal. So is there some way to allow the user to enter the path and
> file themselves? Thanks so much.

> Lilly Irani
> lir...@ix.netcom.com
> --
> ****************************************************
> *Era un da muy sombro. Ella necesitaba un nombre.*
> * El nabo sangriento se llam "el cacto borroso" y *
> *  la luz se apareci. Y, como esto, Spike naci.  *
> * [credit for idea: The {*word*76}y Turnip, canonized   *
> *           saint in Lillican Church]              *
> ****************************************************
> *        http://www.netsurge.com/trillian/         *
> ****************************************************

var filename : string;
     filevar : file of integer;  {can be of any type you want}

begin
  write('Give path and filename: ');
  readln(filename);
  assign(filevar,filename);
  reset(filevar);

     ....... {file manipulations}

  close(filevar);
end.

This will work only if the file already exists or its size isn't zero, in
those cases the program will create a run time error. To avoid those errors
I usually use the following code:

.....
assign(filevar,filename);
{$I-}     {Disables IO-checking}
reset(filevar);
{$I+}    {Re-enables IO-checking}
if ioresult <> 0       {if the file doesn't exist the value of ioresult is
2}
  then
    rewrite(filevar);
........

You mustn't use rewrite(filevar) always, because, if used on an existing
file, is will erase its contents.

Carl

Re:File Handling in Turbo Pascal


Quote
Trillian wrote:
> I'm really sorry but does anyone know where I can find information on
> TurboPascal file handling? I'm trying to write a program that allows
> the
> user to enter the path and file name of a text file and then
> manipulate
> that file. The problem is that in school we only learned Standard
> Pascal. So is there some way to allow the user to enter the path and
> file themselves? Thanks so much.

> Lilly Irani
> lir...@ix.netcom.com
> --
> ****************************************************
> *Era un da muy sombro. Ella necesitaba un nombre.*
> * El nabo sangriento se llam "el cacto borroso" y *
> *  la luz se apareci. Y, como esto, Spike naci.  *
> * [credit for idea: The {*word*76}y Turnip, canonized   *
> *           saint in Lillican Church]              *
> ****************************************************
> *        http://www.netsurge.com/trillian/         *
> ****************************************************

   hiya lilli,
  My name is Mike.  I believe in turbo pascal you should be able just to
ask the user the path and filename as a STRING variable and then use
that string variable in the reset or rewrite statements.  Now i believe
there is no STRING type in standard pascal so is that the problem?  i
think the following should put you on the write track.
program example(input, output, diskfile);
var
  path:string;
  diskfile:text;
begin
  write('Please enter the path: ');
  readln(path);
  assign(diskfile,path);
  reset (diskfile)

etc...
etc...
etc...

that should allow the user to enter the filename and the path to that
file and then the code will access that particular file.
does this help?
Mike Bailey

Other Threads