Recent

Author Topic: Reading text file data into Arrays.  (Read 1052 times)

hymcode

  • New Member
  • *
  • Posts: 20
Re: Reading text file data into Arrays.
« Reply #15 on: January 22, 2021, 10:12:03 am »
If the line in question is not a number (conversion fails) then you decrement the LineNumber.
From that point on the LineArray is out of sync with the actual line numbers in the text file (I think).

Give the following numbers.txt:
Code: [Select]
1
2
3
foo
bar
5
6

When you ask the user for a number and the user enters 4, what should be the response of your program?

Bart



If the user enters 4 the response from the program should be 5. If a line is detected to have an invalid value, I'd like to omit this from being including in the array. For example:

Code: [Select]
1
2
3
foo (skipped)
bar (skipped)
4
5

I hope that helps.
Don’t wish it were easier; wish you were better. – Jim Rohn

Bart

  • Hero Member
  • *****
  • Posts: 4170
    • Bart en Mariska's Webstek
Re: Reading text file data into Arrays.
« Reply #16 on: January 22, 2021, 10:47:45 pm »
But then, why the 2 arrays?
If that is what you want, one will suffice.

Code: Pascal  [Select][+][-]
  1. program filearray;
  2. {$R+}
  3.  
  4. var
  5.   tf : textfile;
  6.   LineText, optionStr : string;
  7.   Q: QWord;
  8.   lineArray : Array [1..100] of QWord;
  9.   lineNum, error_stat, option : Integer;
  10.  
  11. const
  12.   MYFILE = 'NUMBERS.TXT';
  13.  
  14. begin
  15.   Assign(tf, MyFile);
  16.   {$I-}
  17.   Reset(tf);
  18.   error_stat := IOResult;
  19.   if error_stat <> 0 then
  20.   begin
  21.     writeln('Error: cannot open ',MyFile);
  22.     halt(error_stat);
  23.   end;
  24.   LineNum := 0;
  25.   while not Eof(tf) and (LineNum < 100) do
  26.   begin
  27.     Readln(tf, LineText);
  28.     Val(LineText, Q, error_stat);
  29.     if error_stat = 0 then
  30.     begin
  31.       Inc(LineNum);
  32.       LineArray[LineNum] := Q;
  33.     end
  34.   end;
  35.   repeat
  36.     repeat
  37.       write('Enter line number between 1 and ',LineNum,', or 0 to quit: ');
  38.       readln(OptionStr);
  39.       Val(OptionStr, Option, error_stat);
  40.       if error_stat <> 0 then
  41.         writeln(OptionStr,' is not a valid number')
  42.       else
  43.         if (Option < 0) or (Option > LineNum) then
  44.           writeln('Line number must be between 1 and ',LineNum,'.');
  45.       until (error_stat = 0) and (Option >= 0) and  (Option <= LineNum);
  46.      if Option <> 0 then
  47.        writeln(Option,': ',LineArray[Option]);
  48.   until Option = 0;
  49. end.

And with my example numbers.txt:
Code: [Select]
Enter line number between 1 and 4, or 0 to quit: 1
1: 1
Enter line number between 1 and 4, or 0 to quit: 2
2: 2
Enter line number between 1 and 4, or 0 to quit: 3
3: 3
Enter line number between 1 and 4, or 0 to quit: 4
4: 6
Enter line number between 1 and 4, or 0 to quit: 5
Line number must be between 1 and 4.
Enter line number between 1 and 4, or 0 to quit: -999
Line number must be between 1 and 4.
Enter line number between 1 and 4, or 0 to quit: 0

No exceptions needed, less code, less variables.

Bart

ASerge

  • Hero Member
  • *****
  • Posts: 1737
Re: Reading text file data into Arrays.
« Reply #17 on: January 23, 2021, 06:30:14 am »
No exceptions needed, less code, less variables.
Bart, what about closing the file? If the user tries to run a second copy of the program during the dialog, the program will not be able to read the file.

MarkMLl

  • Hero Member
  • *****
  • Posts: 2073
Re: Reading text file data into Arrays.
« Reply #18 on: January 23, 2021, 10:35:05 am »
Bart, what about closing the file? If the user tries to run a second copy of the program during the dialog, the program will not be able to read the file.

Operating-system specific, and OP's given no clue as to what he's using.

OTOH it has already been pointed out to him, so he knows it's something to fix.

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.

egsuh

  • Hero Member
  • *****
  • Posts: 626
Re: Reading text file data into Arrays.
« Reply #19 on: January 23, 2021, 10:55:54 am »
I think most basic codes would look like as following.  I'm just using the inherent functionality of read statement --- which does convert itself.

There are issues of whether more than one figure are allowed in one line. If you want only one number in one line, you may use "readln(f, i);" instead of "read(f, i);"

 I do not know how to write console I/O, so I used TEdit to enter index number, but you may use readln statement if you want console interface. (Also displaying in memo, and using ShowMessage as well).   

Following codes will raise exceptions twice when run within Lazarus IDE (Press continue), but not when run as standalone .exe file.

Code: Pascal  [Select][+][-]
  1. implementation
  2.  
  3. {$R *.lfm}
  4.  
  5. var
  6.    data: array of integer;
  7.  
  8. { TForm1 }
  9.  
  10. procedure TForm1.Button1Click(Sender: TObject);
  11. var
  12.    i :integer;
  13.    f: Textfile;
  14. begin
  15.    assignfile(f, 'd:\tests\data.txt');
  16.    reset(f);
  17.    while not eof(f) do begin
  18.       try
  19.         read (f, i);
  20.         data += [i];
  21.       except
  22.       end;
  23.    end;
  24.    closefile(f);
  25.    for i:= Low(data) to High(data)
  26.        do memo1.lines.add('%d. %d', [i+1, data[i]]);
  27. end;
  28.  
  29. procedure TForm1.Button2Click(Sender: TObject);
  30. var
  31.    i, err: integer;
  32. begin
  33.    val(trim(edit1.text), i, err);
  34.    if err <> 0 then ShowMessage('Please enter number only')
  35.    else if (i > Length(Data)) then ShowMessage('Your index is out of rarnge')
  36.    else ShowMessage(IntToStr(Data[i-1]));
  37. end;
  38.  
  39. end.
  40.  

Content of data.txt file is as follows:
Code: [Select]
101
this
222  33333
44

555555

Bart

  • Hero Member
  • *****
  • Posts: 4170
    • Bart en Mariska's Webstek
Re: Reading text file data into Arrays.
« Reply #20 on: January 23, 2021, 01:03:29 pm »
Bart, what about closing the file? If the user tries to run a second copy of the program during the dialog, the program will not be able to read the file.
Yep, forgot that.

Also, each read from the file should be followed by checking IOResult.

Bart

Bart

  • Hero Member
  • *****
  • Posts: 4170
    • Bart en Mariska's Webstek
Re: Reading text file data into Arrays.
« Reply #21 on: January 23, 2021, 01:14:07 pm »
I think most basic codes would look like as following.
Using dynamic arrays is not the most basic way of doing things.
TS states that even TStringList isn't know in his Pascal dialect, so better stick to things that are widely supported, even in old dialects.
(IIRC then classes in Delphi were introduced before dynamic arrays.)

There are issues of whether more than one figure are allowed in one line.
This was never specified, and the original code did not cater for that possibility, which lead me to the assumption there would ever only be one numer on each line.
Also the "give me a line number and I tell you which number" in it's original wording doesn't use plural.

I do not know how to write console I/O

What about write, writeln, read, readln?
If you omit the textfile parameter, they write to stdout and read from stdin.
I would have thought that was rather basic?

Bart

Bart

  • Hero Member
  • *****
  • Posts: 4170
    • Bart en Mariska's Webstek
Re: Reading text file data into Arrays.
« Reply #22 on: January 23, 2021, 01:19:42 pm »
Bart, what about closing the file? If the user tries to run a second copy of the program during the dialog, the program will not be able to read the file.
Yep, forgot that.

Also, each read from the file should be followed by checking IOResult.

Code: Pascal  [Select][+][-]
  1. program filearray;
  2. {$R+}
  3.  
  4. //uses sysutils;
  5.  
  6. var
  7.   tf : textfile;
  8.   LineText, optionStr : string;
  9.   Q: QWord;
  10.   lineArray : Array [1..100] of QWord;
  11.   lineNum, error_stat, option : Integer;
  12.  
  13. const
  14.   MYFILE = 'NUMBERS.TXT';
  15.  
  16. procedure IOCheck(Msg: String);
  17. var
  18.   Err: Word;
  19. begin
  20.   Err := IOResult;
  21.   if Err <> 0 then
  22.   begin
  23.     writeln(Msg);
  24.     halt(Err);
  25.   end;
  26. end;
  27.  
  28. begin
  29.   Assign(tf, MyFile);
  30.   {$I-}
  31.   Reset(tf);
  32.   IOCheck('Unable to open '+MyFile);
  33.   LineNum := 0;
  34.   while not Eof(tf) and (LineNum < 100) do
  35.   begin
  36.     Readln(tf, LineText);
  37.     IOCheck('Error reading from '+MyFile);
  38.     Val(LineText, Q, error_stat);
  39.     if error_stat = 0 then
  40.     begin
  41.       Inc(LineNum);
  42.       LineArray[LineNum] := Q;
  43.     end
  44.   end;
  45.   Close(tf);
  46.   //Ignore IO error on closing file
  47.   InOutRes := 0;
  48.   repeat
  49.     repeat
  50.       write('Enter line number between 1 and ',LineNum,', or 0 to quit: ');
  51.       readln(OptionStr);
  52.       Val(OptionStr, Option, error_stat);
  53.       if error_stat <> 0 then
  54.         writeln(OptionStr,' is not a valid number')
  55.       else
  56.         if (Option < 0) or (Option > LineNum) then
  57.           writeln('Line number must be between 1 and ',LineNum,'.');
  58.       until (error_stat = 0) and (Option >= 0) and  (Option <= LineNum);
  59.      if Option <> 0 then
  60.        writeln(Option,': ',LineArray[Option]);
  61.   until Option = 0;
  62. end.

Bart

 

TinyPortal © 2005-2018