Link to home
Start Free TrialLog in
Avatar of GunNam
GunNam

asked on

Read or Readln question? looping?

Alright... I have an input file containing the following: (let's call it fiLe)

1001  John 30.0 0.0 0.0 0.0
-1

My pascal program contains:

PROGRAM sample;

VAR a:integer;
       b:string;
       c:real;
       d:real;
       e:real;

BEGIN
      reset(fiLe);
      write('reading...');
      read(fiLe, a, b, c,);  (* I have also tried readln here with the same result *)
END.

Here is the problem I have having.  If I read in just 'a' everything is fine.... it reads "1001"

BUT if I read in 'a' & 'b' ... I get the whole first line of my input file.  When I try to read 3 items from the input file, it tries to read after the end of line or end of file....

Ultimately, I want to have more than one line in the input file with the same format and have it quit reading when it hits a "-1" or "a"

Any help will be greatly appreciated.
     
ASKER CERTIFIED SOLUTION
Avatar of Wim ten Brink
Wim ten Brink
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of GunNam
GunNam

ASKER

If I were to read the whole line into a string, how would I go about getting the values I need out of it.  You mentioned Pos() and Val() but I'm still a bit confused.  
If it were in ANSI C, I could cast the string to an array and compare each array item to a space to get to the next item, but I'm not sure how this works in Pascal.

The real numbers in the input file could change in the amount of places it takes up so I can't bet on the positions always being the same.  
Avatar of GunNam

ASKER

Ok... I just figured out that a string is actually an array and I can access each charactor with it's position in the array.  I'm gonna try and compare each array position to a " " and see if I can atleast get the first item... maybe a while will be better...
Avatar of GunNam

ASKER

the compiler I am using does not support funtions supported by Turbo Pascal... is there a work around for getting stuff out of a string?  Since Pos() and Val() are out now?
Avatar of GunNam

ASKER

I guess the question has now changed to:  How do I go about reading the input file:

1001  John 30.0 0.0 0.0 0.0
-1

where -1 is the sentinel and 6 items are of the type integer, string, real, real, real, real
Avatar of GunNam

ASKER

I just thought of an idea, if someone would help me put it into code.
I could read the first "integer (first 4 digits)" with a regular read(number).

Then for the string, can't I read each character since I know that it will be the next 5 spaces after the space after the "number?"

Next, I'm assuming I could use the regular read command to get the following 4 floating point values...

What do you think?
Avatar of GunNam

ASKER

would reading the file data into a record be a better approach?

I'm thinking read in the integer, then an array of chars then the REAL elements.
Avatar of GunNam

ASKER

So you don't waste your time, I figured out how to read in 1 line.  I'm using a record and using an array of char for the STRING.  

If someone can help me with the sentinel problem, I will still award the full points.
Avatar of GunNam

ASKER

I figured out the looping and reading in all the values.
Avatar of GunNam

ASKER

Workshop_Alex, your comment about how the STRING reads propelled me to figure out how I could read in the rest of the values... thanks.
Cool! Didn't have much time to checkup your problem but I see you managed it quite well by yourself! :-)