Recent

Author Topic: How to write packed word array to csv file?  (Read 1400 times)

tinker101

  • Newbie
  • Posts: 1
How to write packed word array to csv file?
« on: June 23, 2024, 11:42:19 am »
Hi, I'm newbie in Lazarus/Pascal programming.  I tried some examples, but nothing works.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11935
  • FPC developer.
Re: How to write packed word array to csv file?
« Reply #1 on: June 23, 2024, 11:52:31 am »
Hi, I'm newbie in Lazarus/Pascal programming.  I tried some examples, but nothing works.

So what were the error message or wrong behaviour? Did you try to debug? On what OS did you try it with which version?

Some context please.

VisualLab

  • Hero Member
  • *****
  • Posts: 569
Re: How to write packed word array to csv file?
« Reply #2 on: June 23, 2024, 12:28:16 pm »
I tried some examples, but nothing works.

I join marcov's proposal. The task seems simple. Provide an example of the code you used in which the compiler found an error.

Thaddy

  • Hero Member
  • *****
  • Posts: 16158
  • Censorship about opinions does not belong here.
Re: How to write packed word array to csv file?
« Reply #3 on: June 23, 2024, 02:17:23 pm »
do you mean word as in text or word as a 16 bit value?
The latter is simply something like this:
Code: Pascal  [Select][+][-]
  1. program wordtest;
  2. uses sysutils;
  3. var
  4.   f:text;
  5.   a:packed array[0..3] of word  = (1,2,3,4);
  6.   w:word;
  7. begin
  8.   assign(f, 'test.txt');
  9.   rewrite(f);
  10.   for w in a do
  11.     write(f,w.tostring, ',');// writes one comma too many.
  12.   close(f);
  13. end.
Note that is NOT how I would store the data. I  would use file of word, not text.
« Last Edit: June 23, 2024, 03:50:21 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

jamie

  • Hero Member
  • *****
  • Posts: 6734
Re: How to write packed word array to csv file?
« Reply #4 on: June 28, 2024, 01:41:15 am »
Not to be picky but "PACKED array" here implies a multi-dim array which needs to be serialized to a file and reread back in proper order.

 In many languages multiple dim arrays (vectors) are not always contiguous PACKED in memory.

 So, attempting to use a bulk method of content read or write does not work very well in bin mode so using a CSV file seems to fix this.

 I suppose one could force the first items of each line to indicate the index of the dims before issuing the final value.

 Of course, this requires a known fixed dim argument which may not always be desirable.



The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018