Oberon Community Platform Forum
November 18, 2017, 03:30:20 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Array assignment, System.IO methods returning char[] or byte[]  (Read 4645 times)
danp
Newbie
*
Posts: 37


« on: August 08, 2008, 09:55:17 AM »

Hello,

I don't know how to use IO methods returning char[] or byte[]. I'm using System.IO.BinaryReader and I need ReadBytes and ReadChars witch returns byte[] and char[]. How can I use them in Zonnon? What could be the trick to make this array assignment?

Thank you,
Dan
Logged
cfbsoftware
Full Member
***
Posts: 106


WWW
« Reply #1 on: August 09, 2008, 09:06:27 AM »

char[] is equivalent to Zonnon's array * of char

For example the following would read the first twenty characters from file xyz.txt:

Code:
import System.IO as IO;

type buffer = array * of char;
 
var
  buff: buffer;
  r: IO.BinaryReader;
  s: IO.FileStream;
...
...

  s := IO.File.Open('xyz.txt', IO.FileMode.Open, IO.FileAccess.Read);
  r := new IO.BinaryReader(s);
  buff := buffer(r.ReadChars(20));
...
...


Logged

Chris Burrows
Astrobe v6.0 (Jun 2016): Oberon for ARM Cortex-M4 and Cortex-M3 Microcontrollers
http://www.astrobe.com
danp
Newbie
*
Posts: 37


« Reply #2 on: August 09, 2008, 05:27:10 PM »

I have already tried this, but it falls with a run time error:
   Index was outside the bounds of the array.
Do you know any trick to make it works?

Thank you for the answer,
Dan
Logged
danp
Newbie
*
Posts: 37


« Reply #3 on: August 09, 2008, 06:55:02 PM »

I have finally understood: in my code the error is not on the line:
  buf := CharArray(r.ReadChars(ln));
as indicated by the run time message, but on the next line witch converts the char array to a string:
  copy(buf, str);

That because the resulted buf if not 0X ended Zonnon/Oberon ARRAY OF CHAR, as I'm expected. And "copy" fails to convert it to a string, going out of index.

For now, make it works is not very comfortable, and useless CPU time consuming:
Code:
ln := len(buf);
buf1 := new CharArray(ln+1);
for i:=0 to ln-1 do  buf1[i] := buf[i]  end;
buf1[ln] := 0X;
copy(buf1, str);

but I hope Roman will consider to make "copy" works with char arrays returned by System.IO procedures.

Thank you all,
Dan
Logged
cfbsoftware
Full Member
***
Posts: 106


WWW
« Reply #4 on: August 10, 2008, 02:12:22 AM »

I'd be surprised if there was not a more efficient / elegant solution but it is difficult to tell with the limited information given.

e.g. If you are reading a line from a text file and 'ln' is the number of characters in the line then you should be able to say

  buf := CharArray(r.ReadChars(ln+1));

and then say:

  buf[ln] := 0X;

as the last character should be a line-feed which can be replaced by the null. You could then use buf directly - there might be no need to copy it to str.

Otherwise give a general description of the task you are trying to solve and it may be possible to suggest a better approach.
Logged

Chris Burrows
Astrobe v6.0 (Jun 2016): Oberon for ARM Cortex-M4 and Cortex-M3 Microcontrollers
http://www.astrobe.com
danp
Newbie
*
Posts: 37


« Reply #5 on: August 10, 2008, 07:40:54 PM »

I'm not reading lines of text (for that System.IO.StreamReader.ReadLine do a better job), but from a database file. And reading one more char, in most cases, is not advisable. I steel sustain that the efficient/elegant solution could be:

  copy(r.ReadChars(ln), str)

Dan
« Last Edit: August 10, 2008, 07:57:00 PM by danp » Logged
cfbsoftware
Full Member
***
Posts: 106


WWW
« Reply #6 on: August 11, 2008, 01:24:36 AM »

I'm not reading lines of text (for that System.IO.StreamReader.ReadLine do a better job), but from a database file. And reading one more char, in most cases, is not advisable. I steel sustain that the efficient/elegant solution could be:

  copy(r.ReadChars(ln), str)
What is the declaration of str? What do you need to do with the contents of str?
Logged

Chris Burrows
Astrobe v6.0 (Jun 2016): Oberon for ARM Cortex-M4 and Cortex-M3 Microcontrollers
http://www.astrobe.com
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!