Board index » delphi » How to read the file...

How to read the file...

I've got a problem on reading data from a file. The case is like this :
a file called 'BOOK' contains the data :
Title, Author, Publisher, Year, No of pages and Call no.
each group of data is in the same line, eg.
The Book Of Fun John White Goodview Company 1987 125 <eoln>
The Book Of Fun -- Title
John White -- Author
Goodview Company -- Publisher
....
so, how can i write a procedure and read these data and save them in the
variables :
Title, Author, Publisher, Year, NPage, CallNo
where all these variables are of the type of array of string?
How can the program recognize each data? Or what should I do in the file
'BOOK' to seperate them? Hope you can help me.
 

Re:How to read the file...


Quote
> I've got a problem on reading data from a file. The case is like this :
> a file called 'BOOK' contains the data :
> Title, Author, Publisher, Year, No of pages and Call no.
> each group of data is in the same line, eg.
> The Book Of Fun John White Goodview Company 1987 125 <eoln>
> The Book Of Fun -- Title
> John White -- Author
> Goodview Company -- Publisher
> ....
> so, how can i write a procedure and read these data and save them in the
> variables :
> Title, Author, Publisher, Year, NPage, CallNo
> where all these variables are of the type of array of string?
> How can the program recognize each data? Or what should I do in the file
> 'BOOK' to seperate them? Hope you can help me.

   There is no way you can parse the information that you show here
unless there is some special character which delineates the data fields.  
As I look at the example you show above, I see nothing which
distinguishes any single substring (as bounded by a space/whitespace)
from another (except the numeric information), nor is there anything
which indicates that several substrings form a field (e.g. Title).
   If this is a homework project, you should ask the teacher what "field
delimiter" is being used to indicate the termination of individual data
fields.  Without such information, I can't see how to accomplish your
task.

Re:How to read the file...


Quote
butt-kicker wrote:

> I've got a problem on reading data from a file. The case is like this :
> a file called 'BOOK' contains the data :
> Title, Author, Publisher, Year, No of pages and Call no.
> each group of data is in the same line, eg.

Try to read line by line into a string using ReadLn(T,...).
Hopefully it is max. 255 chars!

Then use the string handling procedures of Pascal to extract the
parts, like Copy or Move.
Then remove the trailing blanks from the strings.

Example:

  ReadLn(T,Line);
  if Line > '' then
    Begin
      Title[0] := #20;
      Move(Line[1],Title[1],20);
      Author[0] := #16;
      Move(Line[21],Author[1],16);
      ...
    End;
This is a primitive approach, a function would be better:
  Function Extract(Posi:Integer;MaxLen:Integer):String;
  Var S : String;
  Begin
    S[0] := Chr(MaxLen);
    Move(Line[Posi],S[1],MaxLen);
    while (S > '') and (S[length(S)] = ' ' do
      Dec(S[0]);
    Extract := S;
  End;
More basics in the FAQ chapters, information, source etc. of the
TP-links:
http://www.geocities.com/SiliconValley/2926/tpf.html
--
Franz Glaser, Glasau 3, A-4191 Vorderweissenbach Austria ++43-7219-7035
Muehlviertler Elektronik Glaser. Industrial control and instrumentation
-----------------------------------------------------------------------
http://members.eunet.at/meg-glaser           mailto:meg-gla...@eunet.at
http://www.geocities.com/~franzglaser  http://members.xoom.com/f_glaser

Re:How to read the file...


Hello butt-kicker!

You have no chance to do it the manner you described. There are in general
two ways to do such an import:

   1. You have a field separator like ';'
   2. The field length is determined

In your example there is no field sep and it is impossible to prodict the
offset and length of each field.

--

Regards

G.Pohl

**********************************************************************
eMail: g...@rp-plus.de
Homepage: http://www.online-club.de/~Eulenspiegel
Phone: +49 2161 593157
**********************************************************************

butt-kicker schrieb in Nachricht <36EB7805.AB12D...@hknet.com>...
:I've got a problem on reading data from a file. The case is like this :
:a file called 'BOOK' contains the data :
:Title, Author, Publisher, Year, No of pages and Call no.
:each group of data is in the same line, eg.
:The Book Of Fun John White Goodview Company 1987 125 <eoln>
:The Book Of Fun -- Title
:John White -- Author
:Goodview Company -- Publisher
:....
:so, how can i write a procedure and read these data and save them in the
:variables :
:Title, Author, Publisher, Year, NPage, CallNo
:where all these variables are of the type of array of string?
:How can the program recognize each data? Or what should I do in the file
:'BOOK' to seperate them? Hope you can help me.
:

Re:How to read the file...


Re:How to read the file...


Thanks you all.
Quote
Mike Copeland wrote:
> > I've got a problem on reading data from a file. The case is like this :
> > a file called 'BOOK' contains the data :
> > Title, Author, Publisher, Year, No of pages and Call no.
> > each group of data is in the same line, eg.
> > The Book Of Fun John White Goodview Company 1987 125 <eoln>
> > The Book Of Fun -- Title
> > John White -- Author
> > Goodview Company -- Publisher
> > ....
> > so, how can i write a procedure and read these data and save them in the
> > variables :
> > Title, Author, Publisher, Year, NPage, CallNo
> > where all these variables are of the type of array of string?
> > How can the program recognize each data? Or what should I do in the file
> > 'BOOK' to seperate them? Hope you can help me.

> ?? There is no way you can parse the information that you show here
> unless there is some special character which delineates the data fields.
> As I look at the example you show above, I see nothing which
> distinguishes any single substring (as bounded by a space/whitespace)
> from another (except the numeric information), nor is there anything
> which indicates that several substrings form a field (e.g. Title).
> ?? If this is a homework project, you should ask the teacher what "field
> delimiter" is being used to indicate the termination of individual data
> fields.? Without such information, I can't see how to accomplish your
> task.

Other Threads