Recent

Author Topic: Save and write whole arrays of numbers to the text files  (Read 11611 times)

BobDog

  • Sr. Member
  • ****
  • Posts: 394
Re: Save and write whole arrays of numbers to the text files
« Reply #15 on: September 27, 2021, 11:32:49 am »

Hi wp
I think you just have to get into a matrix mode for the (type matrix) and throw extreme caution to the wind.
I have tried a matrix object in other languages, it is all very well, but I always have preferred straight arrays for matrix operations, they are faster and cleaner IMHO.
My previous matrix mode in this forum:
https://forum.lazarus.freepascal.org/index.php/topic,55254.msg410793.html#msg410793
And of course the dynamic array approach is better I think.
Just look upon the operation as using a subset of the pointer.
But then again nobody responded to my matrix in that post, so you might have the better idea after all.





Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Save and write whole arrays of numbers to the text files
« Reply #16 on: September 27, 2021, 11:40:35 am »
You really do not have to use msvcrt since Pascal has all the equivalents for the filehandling you do here.
See the examples in the manual.
Here is one of them:
Code: Pascal  [Select][+][-]
  1. Program Example6;
  2.  
  3. { Program to demonstrate the BlockRead and BlockWrite functions. }
  4.  
  5. Var Fin, fout : File;
  6.     NumRead,NumWritten : Word;
  7.     Buf : Array[1..2048] of byte;
  8.     Total : Longint;
  9.  
  10. begin
  11.   Assign (Fin, Paramstr(1));
  12.   Assign (Fout,Paramstr(2));
  13.   Reset (Fin,1);
  14.   Rewrite (Fout,1);
  15.   Total:=0;
  16.   Repeat
  17.     BlockRead (Fin,buf,Sizeof(buf),NumRead);
  18.     BlockWrite (Fout,Buf,NumRead,NumWritten);
  19.     inc(Total,NumWritten);
  20.   Until (NumRead=0) or (NumWritten<>NumRead);
  21.   Write ('Copied ',Total,' bytes from file ',paramstr(1));
  22.   Writeln (' to file ',paramstr(2));
  23.   close(fin);
  24.   close(fout);
  25. end.
The link is here and subsequent links can be found there too:
https://www.freepascal.org/docs-html/rtl/system/blockread.html
This is also fully cross-platform and only uses the system unit.

But as suggested,  TFilestream is a bit more convenient.
« Last Edit: September 27, 2021, 11:42:23 am by Thaddy »
Specialize a type, not a var.

 

TinyPortal © 2005-2018