Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

arrays..reading into a program and displaying

jettamanjettaman Member Posts: 19
Hi.. I'm trying to write a program which uses an array.
I'm trying to read a text.file into an array and then display it but I can only get it to displace the first character of the first line and the first character of the second line.

Any suggestion:
Thanks

uses
SysUtils;

CONST
Min = 1;
Max = 75;

Type
Vector = Array [Min..Max] of Char;

{******************************************************************************}

PROCEDURE GetFile (VAR Infile:Text);

VAR
FileName : String;

Begin
Writeln;
Writeln ('Enter the path and filename for the file to be processed: ');
Readln (Filename);
Assign (Infile, Filename);
Reset (Infile);
End;

{******************************************************************************}

PROCEDURE ReadData (VAR Infile: text; VAR List:Vector; VAR count: integer);

Begin
count := 0;
While not EOF(Infile) Do
Begin
count := count + 1;
readln (infile, List[count]);
writeln (list[count]);
end;
End;

{******************************************************************************}

PROCEDURE Displayresults (VAR List: Vector; Count:Integer);

VAR
Index: integer;

Begin
Writeln;
FOR Index := Min to count Do
writeln;
Write (List[count]);
writeln;


end;

{******************************************************************************}




VAR
List : Vector;
Count : integer;
Infile : text;


Begin
GetFile (Infile);
ReadData (Infile, List, Count);
displayresults (list,count);
writeln('Push ENTER to exit');
readln;
End.


Comments

  • ibyiby Member Posts: 323
    [code]

    { uses SysUtils; what is this and what do you need it for? }

    CONST Min = 1; { you should declare TYPE like " const Min:BYTE = 1; " }
    Max = 75;

    Type Vector = Array [Min..Max] of string;
    { using CHAR will give you only
    FIRST character of the line you read -> replace with STRING }

    {******************************************************************************}

    PROCEDURE GetFile (VAR Infile:Text);

    VAR
    FileName : String;

    Begin
    Writeln;
    Writeln ('Enter the path and filename for the file to be processed: ');
    Readln (Filename);
    Assign (Infile, Filename);
    Reset (Infile);
    End;

    {******************************************************************************}

    PROCEDURE ReadData (VAR Infile: text; VAR List:Vector; VAR count: integer);

    Begin
    count := 0;
    While not EOF(Infile) Do
    Begin
    count := count + 1;
    readln (infile, List[count]);
    { writeln (list[count]); }
    end;
    End;

    {******************************************************************************}

    PROCEDURE Displayresults (VAR List: Vector; Count:Integer);

    VAR
    Index: integer;

    Begin
    Writeln('Here is what we''ve got...');
    FOR Index := Min to count Do
    { writeln; take this line out - it writes empty line COUNT times
    so if count was 30, it will scroll down the screen 30 lines which
    effectivelly cleares the screen }

    WriteLn(List[count]); { better use writeln alone, it's faster }
    { writeln; }
    end;

    {******************************************************************************}




    VAR
    List : Vector;
    Count : integer;
    Infile : text;


    Begin
    GetFile (Infile);
    ReadData (Infile, List, Count);
    displayresults (list,count);
    writeln('Push ENTER to exit');
    readln;
    [b]CLOSE(InFile);[/b] { You must close file after use }
    End.
    [/code]


    Iby
  • slicer69slicer69 Member Posts: 272
    You're only problem seems to be the way you are reading data into the array. readln() does not stop until it hits a newline.
    So trying to read in a character (char) with readln
    works only is the line is one character long. Try
    using readln(file, full_line);
    or blockread(file, full_line, line_length);

    Try using this as your reading in procedure:


    PROCEDURE ReadData (VAR Infile: text; VAR List:Vector; VAR count: integer);

    Begin
    count := 0;
    While not EOF(Infile) Do
    Begin
    count := count + 1;
    readln (infile, List);
    { readln() does not stop until it hits a newline }
    end;
    End;



  • jettamanjettaman Member Posts: 19
    Just wanted to say thanks for the help on this one.


    : You're only problem seems to be the way you are reading data into the array. readln() does not stop until it hits a newline.
    : So trying to read in a character (char) with readln
    : works only is the line is one character long. Try
    : using readln(file, full_line);
    : or blockread(file, full_line, line_length);
    :
    : Try using this as your reading in procedure:
    :
    :
    : PROCEDURE ReadData (VAR Infile: text; VAR List:Vector; VAR count: integer);
    :
    : Begin
    : count := 0;
    : While not EOF(Infile) Do
    : Begin
    : count := count + 1;
    : readln (infile, List);
    : { readln() does not stop until it hits a newline }
    : end;
    : End;
    :
    :
    :
    :

  • Shawn CarterShawn Carter Member Posts: 0

    / http://forcoder.org / free ebooks and video tutorials about || Swift PL/SQL Ruby C Java Delphi C++ Perl JavaScript Python MATLAB Visual Basic .NET Objective-C Go R PHP Scratch Assembly Visual Basic C# SAS Crystal Alice Hack Scheme LabVIEW Prolog VBScript ML Transact-SQL Ada Julia COBOL ABAP Fortran Lua Dart F# D Logo Rust Scala Bash FoxPro Awk Clojure Apex Kotlin Lisp Erlang || ______

Sign In or Register to comment.