How can I read text files into variables? 
Author Message
 How can I read text files into variables?

I need to get Pascal to read the contents of a text file into multiple
variables, one per line.

Matt

PS Im using Prospero



Sun, 29 Sep 2002 03:00:00 GMT  
 How can I read text files into variables?
Sounds like you need a simple parser.  What "rules" define when you stop
reading?

    WHILE NOT eof(inputfile) DO
        BEGIN
          initializenextvariable;
          WHILE NOT terminator(inputfile) DO
             readintovariable (inputvariable, inputfile);
         END;

The details, for example, what the variables are, how you initialize them,
what a terminator is (for example, if you were reading lines into
variables, the terminator would be the end-of-line condition), etc. are up
to you to decide.

Bob Schor
Pascal Enthusiast

P.S. -- I really like the Prospero compiler ...

Quote:

> I need to get pascal to read the contents of a text file into multiple
> variables, one per line.

> Matt

> PS Im using Prospero



Sun, 29 Sep 2002 03:00:00 GMT  
 How can I read text files into variables?


Quote:
>I need to get pascal to read the contents of a text file into multiple
>variables, one per line.

>Matt

>PS Im using Prospero

This is actually very easy.
Say your file looks like this:
 *data.txt:
 123
 434
 342
 343

Meaning there's one integer per line.

try the following code:
var
 n:array[1..100] of integer;
 nr:integer;
 f:text;

begin
 nr := 0;
 assign(f,'data.txt');
 reset(f);
 while not eof(f) do
  begin
    inc(nr);
    readln(f,n[nr]);  {this can also be just read and not readln!}
  end;
close(f);
{Ok know the stuff is in the array}

It works pretty much the same with other types of variables.
one more e.g.:
 file:
 23 23 245 12

(four numbers in one line)
readin with statment:
 readln(f,num1,num2,num3,num4);

or
 for i := 1 to 4 do
  read(f,n[i]);



Mon, 30 Sep 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Read variables from text file?

2. Text File Read / Dbase file Write

3. Reading different size variables from same open file in Turbo Pascal 3.0

4. Save/read variable values in a file

5. reading a Unix Generated text file

6. How can read a text file?

7. Reading a text file

8. Read an external TEXT files and store info in 2 Separate ARRAYS

9. read text file

10. How to be reading a text file

11. Reading info from text file...

12. reading from text files

 

 
Powered by phpBB® Forum Software