Files in Pascal Please help 
Author Message
 Files in Pascal Please help

Hi there!
i need some help with handling files in Pascal.
I want to create a file with many fields.
For example one field is a number the other is the name the other
the grade etc.
How will i be able to read those fields ?
Can i only read one character a time or all the string or integer
by once ?
One more question:is there a way i can write to a file without
having used rewrite in the beginning and if yes how?

Many thanks,
gwtt

--== Sent via Deja.com http://www.*-*-*.com/
---Share what you know. Learn what you don't.---



Wed, 18 Jun 1902 08:00:00 GMT  
 Files in Pascal Please help

Quote:

> i need some help with handling files in pascal.
> I want to create a file with many fields.
> For example one field is a number the other is the name the other
> the grade etc.
> How will i be able to read those fields ?
> Can i only read one character a time or all the string or integer
> by once ?
> One more question:is there a way i can write to a file without
> having used rewrite in the beginning and if yes how?

First define a RECORD, then fill the record in MEMORY with
your data. Define a file of that record and write it to disk.
The first point: forget using text files.
Second point: read your manuals (hope you have!) for record types,
file of any type, the seek procedure etc.
--
Franz Glaser, Glasau 3, A-4191 Vorderweissenbach Austria +43-7219-7035-0
Muehlviertler Elektronik Glaser.  Industrial control and instrumentation

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


Wed, 18 Jun 1902 08:00:00 GMT  
 Files in Pascal Please help

Quote:

> Hi there!
> i need some help with handling files in pascal.
> I want to create a file with many fields.
> For example one field is a number the other is the name the other
> the grade etc.
> How will i be able to read those fields ?
> Can i only read one character a time or all the string or integer
> by once ?
> One more question:is there a way i can write to a file without
> having used rewrite in the beginning and if yes how?

     In Standard Pascal, there are two basic types of files.  One,
called "text files" (declared with the type "text"), are
"human-readable", that is, they consist of characters
that represent values, i.e. the characters "1", "2", and "3" together
representing the number
123.  The other type, sometimes called "binary files", consist of
representations of machine data, and are not easily read by humans (but
are easily read by machines).  Here,
if you wrote the number "123", you'd be writing a 16-bit (or 32-bit)
chunk of data that would represent the number 123.

     When you write text files, you write out whatever you want, freely
mixing integers, reals, text, as you wish, and dividing your output by
writing "lines" (with the "writeln()" statement) when you wish.  There's
no requirement that each line be the same, so you
could write a header line (like "Number   Unit Cost   Total Cost") then
lines of numbers
(like 5         1.03        5.15).

     When you write binary files, you decide on the basic file format,
which is then repeated throughout the file.  If your data can be
described by a complicated record, with
various integers, reals, characters, arrays, etc., then you can write
out one such record with each output.  You tend to use put() and get()
with binary files, with each call returning (or outputting) one record.

     Thus if you have a file with many fields, and you want to store it
(like on a disk), a
FILE OF manyfields (where "manyfields" is what I'm calling your record
of many fields)
would seem to be appropriate.  Simply define each record, then write it
(or read it).
To write it to the screen, you would get the next record, then write
out, formatting as you
wish, each of the elements to the screen.

     Now, can you write a file without doing a rewrite()?  Basically,
no.  What the rewrite() does for you is to associate your internal
Pascal file variable (for example,
VAR myfile : FILE OF manyfields, or VAR yourfile : text) with a specific
external file
that has a particular name.  Actually, that's not quite accurate.  The
method by which this association, or "binding", takes place is another
one of those places the Pascal Standard is a bit fuzzy.  But never
mind.  The other thing rewrite() does for you is to tell the program
(and the file) "I'm now starting to write.  Start outputting from the
beginning of the file.  If there was anything there before, consider it
erased, because I'm going to overwrite it".  If you are going to write a
file, you really do have to start at the beginning, and that is the
function of "rewrite()".

Bob Schor
Pascal Enthusiast



Wed, 18 Jun 1902 08:00:00 GMT  
 Files in Pascal Please help
Bob, what about "append()"? That begins writing at the end of the file,
though...
Quote:
> > One more question:is there a way i can write to a file without
> > having used rewrite in the beginning and if yes how?
> erased, because I'm going to overwrite it".  If you are going to write a
> file, you really do have to start at the beginning, and that is the
> function of "rewrite()".

> Bob Schor
> Pascal Enthusiast



Wed, 18 Jun 1902 08:00:00 GMT  
 Files in Pascal Please help
   Append applies only to Text files.  The original post(s) didn't
specify Text files, so that solution can't be assumed.  
Quote:
> Bob, what about "append()"? That begins writing at the end of the file,
> though...

> > > One more question:is there a way i can write to a file without
> > > having used rewrite in the beginning and if yes how?

> > erased, because I'm going to overwrite it".  If you are going to write a
> > file, you really do have to start at the beginning, and that is the
> > function of "rewrite()".



Wed, 18 Jun 1902 08:00:00 GMT  
 Files in Pascal Please help
Hi,

If you want to keep existing data in the file, use
  reset(FileVar)
If you want to create a new file, or erase all the data in an existing
file, use
  rewrite(FileVar)
If you want to open a file if it already exists, but create a new file if
it doesn't exists, use:
  {$I-} reset(FileVar); {$I+}
  if (IOResult <> 0) then
    rewrite(FileVar);

In your type- and var-section, you have something like:

type
  TStudent = record
     name : string[20];
     etc etc
  end;

var
  FileVar : file of TStudent

begin
  assign(FileVar, 'name of file on the disk');
  { now the stuff above ... }
end.

Hope this somehow helps you.

Bye
Al.



Quote:
> Bob, what about "append()"? That begins writing at the end of the file,
> though...

> > > One more question:is there a way i can write to a file without
> > > having used rewrite in the beginning and if yes how?

> > erased, because I'm going to overwrite it".  If you are going to write
a
> > file, you really do have to start at the beginning, and that is the
> > function of "rewrite()".

> > Bob Schor
> > Pascal Enthusiast



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

 Relevant Pages 

1. Files in Pascal Please help

2. Graph.tpu file wanted please, please, please.

3. HELP ! HELP PLEASE IN COMPILE TO EXE FILE

4. Help Me Please (S.O.S. pascal) -- load bmp images using pascal

5. Graph.tpu for pascal wanted please, please, please.

6. Please help with log files Uprgent !!!

7. Help please: non-BDE, non-ODBC database files

8. help please in reading .dat files

9. Lock file has grown too large - please help

10. please help - create a batch file to create folders

11. Parameter passing to othetr EXE file, please help...

12. Please help with Text File reading...

 

 
Powered by phpBB® Forum Software