Recent

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

hymcode

  • New Member
  • *
  • Posts: 20
Reading text file data into Arrays.
« on: January 21, 2021, 03:11:18 pm »
Hello everyone,
I have a text file with some random numbers on each line, and I'd like to be able to display them with a number beside and make them selectable. Shown below is currently what I have, but I'm not sure if this is the correct method and as it stands I am unable to select the value I want.

Updated: 22/01/21
Code: Pascal  [Select][+][-]
  1. program fileArray;
  2.  
  3. uses sysutils;
  4.  
  5. var
  6.   tf : textfile;
  7.   lineTxt, optionStr : string;
  8.   textArray : Array [1..100] of QWord;
  9.   lineArray : Array [1..100] of QWord;
  10.   lineNum, lineText, error_stat, option : Integer;
  11.  
  12. const
  13.   MYFILE = 'NUMBERS.TXT';
  14.  
  15. begin
  16.   lineNum := 0;
  17.   lineText := 0;
  18.   error_stat := 0;
  19.   writeln('Reading files into an array');
  20.  
  21.   {$I-}
  22.   AssignFile(tf, MYFILE);
  23.   {$I+}
  24.   error_stat := IOResult;
  25.  
  26.   if error_stat = 0 then
  27.   begin
  28.     writeln('File assign successful');
  29.  
  30.     {$I-}
  31.     reset(tf);
  32.     {$I+}
  33.     error_stat := IOResult;
  34.  
  35.     if error_stat = 0 then
  36.     begin
  37.       writeln('File reset successful');
  38.       while not eof (tf) do
  39.  
  40.       try
  41.         begin
  42.           readln(tf, linetxt);
  43.  
  44.           lineNum := lineNum + 1;
  45.  
  46.           textArray[lineText] := StrToQWord(lineTxt);
  47.  
  48.           writeln(lineArray[lineNum], '. ', textArray[lineNum]);
  49.         end;
  50.       except
  51.         on Exception : EConvertError do
  52.         begin
  53.           lineNum:=  lineNum - 1;
  54.         end;
  55.       end;
  56.  
  57.       writeln('Select a number: ');
  58.       readln(optionStr);
  59.  
  60.       {$I-}
  61.       readStr(optionStr, option);
  62.       {$I+}
  63.       error_stat := IOResult;
  64.  
  65.       if error_stat = 0 then
  66.       begin
  67.         {need to select the number to get the corresponding line from text file}
  68.          if ((option < 1) or (option > lineNum))then
  69.          begin
  70.            writeln('below 1 or greater than ', lineNum);
  71.          end
  72.          else
  73.          begin
  74.            writeln('it works');
  75.            writeln(lineArray[option], '. ', textArray[option]);
  76.          end;      
  77.       end
  78.       else
  79.       begin
  80.         writeln('Incorrect Input');
  81.       end;
  82.     end;
  83.  
  84.     {$I-}
  85.     close(tf);
  86.     {$I+}
  87.     error_stat := IOResult;
  88.  
  89.     if error_stat = 0 then
  90.     begin
  91.       writeln('File closed');
  92.     end
  93.     else
  94.       writeln('File cannot be closed');  
  95.  
  96.   end;
  97.   readln;
  98. end.

Output:
Code: Text  [Select][+][-]
  1. Reading files into an array
  2. File assign successful
  3. File reset successful
  4. 1. 123
  5. 2. 345
  6. 3. 789
  7. Select a number:
  8. 2
  9. it works
  10. 2. 345
  11. File closed

What I hope to achieve is being able to select a number which corresponds to the line position in the text file, while making sure that any invalid lines are skipped. I hope this makes sense and would really appreciate some advice.


Update: 22/01/2021
I think I have got my intended output in order now. Just need to do some more testing.
« Last Edit: January 22, 2021, 04:02:13 pm by hymcode »
Don’t wish it were easier; wish you were better. – Jim Rohn

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Reading text file data into Arrays.
« Reply #1 on: January 21, 2021, 03:21:29 pm »
Looks serviceable, but also note the existence of TryStrToQWord() and StrToQWordDef() etc.

If you want to do more complex checking then looking at regular expressions might possibly be justified.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

hymcode

  • New Member
  • *
  • Posts: 20
Re: Reading text file data into Arrays.
« Reply #2 on: January 21, 2021, 04:00:45 pm »
Looks serviceable, but also note the existence of TryStrToQWord() and StrToQWordDef() etc.

If you want to do more complex checking then looking at regular expressions might possibly be justified.

MarkMLl



Thanks for the feedback and for the suggestions regarding TryStrToQWord() and StrToQWordDef(). Would you happen to know how I will now be able to make my number options now selectable by the user?
Don’t wish it were easier; wish you were better. – Jim Rohn

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Reading text file data into Arrays.
« Reply #3 on: January 21, 2021, 04:04:46 pm »
Thanks for the feedback and for the suggestions regarding TryStrToQWord() and StrToQWordDef(). Would you happen to know how I will now be able to make my number options now selectable by the user?

Yes, all you have to do is write code for it.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

ASerge

  • Hero Member
  • *****
  • Posts: 2222
Re: Reading text file data into Arrays.
« Reply #4 on: January 21, 2021, 05:59:28 pm »
Looks serviceable, but also note the existence of TryStrToQWord() and StrToQWordDef() etc.
I wouldn't call it serviceable. The file is not closed, and the input outside the array boundary is not checked. It is better to immediately learn how to write correct programs.

Bart

  • Hero Member
  • *****
  • Posts: 5274
    • Bart en Mariska's Webstek
Re: Reading text file data into Arrays.
« Reply #5 on: January 21, 2021, 06:22:40 pm »
Why don't you read the entire file into a TStringList?
No need to have 2 arrays for indexing.

Bart

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Reading text file data into Arrays.
« Reply #6 on: January 21, 2021, 06:28:54 pm »
Cut him some slack, chaps, this is the beginner's area and I'm sure that he'll tidy things up as he accumulates experience :-)

@ASerge: an example would be useful, even if it were purely an outline like

Code: Pascal  [Select][+][-]
  1. open(...
  2. try
  3. // Stuff here
  4. finally
  5.   Close(...
  6. end;
  7.  

@Bart: There's a distinct possibility that he doesn't know about it since he might be working from an old Turbo Pascal book or a generic Pascal example.

OP: You /will/ tidy things up eventually, won't you? :-)

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

hymcode

  • New Member
  • *
  • Posts: 20
Re: Reading text file data into Arrays.
« Reply #7 on: January 21, 2021, 06:31:15 pm »
Why don't you read the entire file into a TStringList?
No need to have 2 arrays for indexing.

Bart


I am making a console program which I would also like to translate into a HP Pascal program, which to the best of my knowledge does not support TStringList
« Last Edit: January 21, 2021, 06:37:58 pm by hymcode »
Don’t wish it were easier; wish you were better. – Jim Rohn

hymcode

  • New Member
  • *
  • Posts: 20
Re: Reading text file data into Arrays.
« Reply #8 on: January 21, 2021, 06:37:18 pm »
Looks serviceable, but also note the existence of TryStrToQWord() and StrToQWordDef() etc.
I wouldn't call it serviceable. The file is not closed, and the input outside the array boundary is not checked. It is better to immediately learn how to write correct programs.



Thank you for noting that I have not closed my file. I've made the changes for this. But I am unsure what you mean by: "the input outside the array boundary is not checked"
Don’t wish it were easier; wish you were better. – Jim Rohn

ASerge

  • Hero Member
  • *****
  • Posts: 2222
Re: Reading text file data into Arrays.
« Reply #9 on: January 21, 2021, 06:40:21 pm »
Thank you for noting that I have not closed my file. I've made the changes for this. But I am unsure what you mean by: "the input outside the array boundary is not checked"
What happens when you read 101 lines of data?

hymcode

  • New Member
  • *
  • Posts: 20
Re: Reading text file data into Arrays.
« Reply #10 on: January 21, 2021, 06:53:58 pm »
Thank you for noting that I have not closed my file. I've made the changes for this. But I am unsure what you mean by: "the input outside the array boundary is not checked"
What happens when you read 101 lines of data?



Oh, right I see, sorry I was being very slow there. Yes I haven't added any checks against data going outside the boundaries of my array. My current focus at the moment is trying to understand how I can select one of the numbers displayed on the screen, but thank you for pointing this out to me and I hope to address it at some point. 
Don’t wish it were easier; wish you were better. – Jim Rohn

Bart

  • Hero Member
  • *****
  • Posts: 5274
    • Bart en Mariska's Webstek
Re: Reading text file data into Arrays.
« Reply #11 on: January 21, 2021, 06:56:11 pm »
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
« Last Edit: January 21, 2021, 07:16:18 pm by Bart »

Bart

  • Hero Member
  • *****
  • Posts: 5274
    • Bart en Mariska's Webstek
Re: Reading text file data into Arrays.
« Reply #12 on: January 21, 2021, 07:13:09 pm »
Also the first time you do
Code: Pascal  [Select][+][-]
  1. textArray[lineText] := StrToQWord(lineTxt);
LineText is zero, so instant runtime error.

Oh, and having a variable LineTex and a variable LineTxt is rather confusing when inspecting the code...

Bart

Bart

  • Hero Member
  • *****
  • Posts: 5274
    • Bart en Mariska's Webstek
Re: Reading text file data into Arrays.
« Reply #13 on: January 21, 2021, 07:21:10 pm »
Fixing the bug above you get (given the numvers.txt 2 posts above):
Code: [Select]
C:\Users\Bart\LazarusProjecten\bugs\forum\linearray>filearray
Reading files into an array
File assign successful
File reset successful
1. 1
2. 2
3. 3
4. 6
Select a number:
1
LineArray: 1
TextArray: 1
File closed


C:\Users\Bart\LazarusProjecten\bugs\forum\linearray>filearray
Reading files into an array
File assign successful
File reset successful
1. 1
2. 2
3. 3
4. 6
Select a number:
2
LineArray: 2
TextArray: 2
File closed


C:\Users\Bart\LazarusProjecten\bugs\forum\linearray>filearray
Reading files into an array
File assign successful
File reset successful
1. 1
2. 2
3. 3
4. 6
Select a number:
3
LineArray: 3
TextArray: 3
File closed


C:\Users\Bart\LazarusProjecten\bugs\forum\linearray>filearray
Reading files into an array
File assign successful
File reset successful
1. 1
2. 2
3. 3
4. 6
Select a number:
6
LineArray: 0
TextArray: 6
File closed

Does that output make sense to you?

Bart

Bart

  • Hero Member
  • *****
  • Posts: 5274
    • Bart en Mariska's Webstek
Re: Reading text file data into Arrays.
« Reply #14 on: January 21, 2021, 10:27:26 pm »
I am making a console program which I would also like to translate into a HP Pascal program, which to the best of my knowledge does not support TStringList

Does it support ReadStr() (which is ISO Extended Pascal, according to the fpc docs)?

The default conversion routines number<->string are Val() and Str().
If you use these, you don't need a try..except block.

What I hope to achieve is being able to select a number which corresponds to the line position in the text file, while making sure that any invalid lines are skipped.

What exactly do you mean by that?
Should it show the number on the X-th line in the text file?
Should it show the X-th valid entry in the text file?
Do invalid lines matter when yoy say "corrsponds to the line position in the text file"?

There's to much ambiguity.

Bart

 

TinyPortal © 2005-2018