Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Read or Readln question?  looping?

Posted on 2003-10-28
11
Medium Priority
?
423 Views
Last Modified: 2012-05-04
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.
     
0
Comment
Question by:GunNam
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 2
11 Comments
 
LVL 17

Accepted Solution

by:
Wim ten Brink earned 200 total points
ID: 9641287
That's because reading a string means reading until the end of a line. The second value type you read is a string and thus it contains everything up until the last line.

It might be more useful to just use a temporary string, use ReadLn(AFile, TempStr) after declaring the var TempStr as a string and then split the string you've just read into the different variables you have. The functions Pos() and Val() will be very helpful with this.
0
 

Author Comment

by:GunNam
ID: 9641420
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.  
0
 

Author Comment

by:GunNam
ID: 9641473
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...
0
[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

 

Author Comment

by:GunNam
ID: 9641528
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?
0
 

Author Comment

by:GunNam
ID: 9641562
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
0
 

Author Comment

by:GunNam
ID: 9641702
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?
0
 

Author Comment

by:GunNam
ID: 9642322
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.
0
 

Author Comment

by:GunNam
ID: 9643107
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.
0
 

Author Comment

by:GunNam
ID: 9643677
I figured out the looping and reading in all the values.
0
 

Author Comment

by:GunNam
ID: 9643689
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.
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 9644392
Cool! Didn't have much time to checkup your problem but I see you managed it quite well by yourself! :-)
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If something goes wrong with Exchange, your IT resources are in trouble.All Exchange server migration processes are not designed to be identical and though migrating email from on-premises Exchange mailbox to Cloud’s Office 365 is relatively simple…
Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question