Board index » delphi » 2d array file errors

2d array file errors

hi all

I have this problem. I am trying to write to a file useing a 2d array
in a loop but I keep getting a type mismatch error
the procedures are below.

the type mismatch error I get when compiling is at the line
write(datas,datastring[a1,b1]);
in the save procedure

and the  same type mismatch erro in the load procedure at
line

   read(datas,datastring[a11,b11]);

does this mean that a 2d array cannot be writen to file ?

I have set the varables for both of the files the same type as the
array ... I was wondering if the file varable has to be defined
diferently since it is a 2d array of string ?

does any kind sould know where I am going wrong ?

I would be eternaly in your debt if you could help me with this one
thanks in advance

var
   datas                                  : file of string;
   crf                                    : file of integer;
   stp                                    : string;
   a,b,c,cols,rows,rloop,cloop            : integer;
procedure save;
var
   a1 : integer;
   b1 : integer;
begin
     assign(crf,'c:/crinfo.wap');
     rewrite(crf);
     write(crf,cols);
     write(crf,rows);
     close(crf);
     assign(datas,'c:/pasdem.wap');
     rewrite(datas);
     for a1 := 1 to rows do
     begin
          for b1 := 1 to cols do
          begin
               write(datas,datastring[a1,b1]);
          end;
     end;
     close(datas);
end;

procedure load;
var
   a11,b11     : integer;
begin
     assign(crf,'c:/crinfo.wap');
     reset(crf);
     while not eof(crf) do
     begin
          read(crf,cols);
          read(crf,rows);
     end;
     close(crf);
     assign(datas,'c:/pasdem.wap');
     reset(datas);
     while not eof(datas) do
     begin
          for a11 := 1 to rows do
          begin
               for b11 := 1 to cols do
               begin
                     read(datas,datastring[a11,b11]);
               end;
          end;
     end;
     close(datas);
end;

thanks again

 

Re:2d array file errors


In article <vqsmascoqhut8g8d8b4bvnistvomp2k...@4ax.com>,
  _meow <b...@halligan74.junglelink.co.uk> wrote:

Quote
> hi all

> I have this problem. I am trying to write to a file useing a 2d array
> in a loop but I keep getting a type mismatch error
> the procedures are below.

> the type mismatch error I get when compiling is at the line
> write(datas,datastring[a1,b1]);
> in the save procedure

> and the  same type mismatch erro in the load procedure at
> line

>    read(datas,datastring[a11,b11]);

> does this mean that a 2d array cannot be writen to file ?

> I have set the varables for both of the files the same type as the
> array ... I was wondering if the file varable has to be defined
> diferently since it is a 2d array of string ?

> does any kind sould know where I am going wrong ?

> I would be eternaly in your debt if you could help me with this one
> thanks in advance

> var
>    datas                                  : file of string;
>    crf                                    : file of integer;
>    stp                                    : string;
>    a,b,c,cols,rows,rloop,cloop            : integer;
> procedure save;

<snip>

After adding

var datastring: array [1..4, 1..4] of string; {at the top}

and

begin
end. {at the bottom}

It compiles cleanly, so you need to give more information.

Robert
--
Robert AH Prins
prin...@willis.com

Sent via Deja.com http://www.deja.com/
Before you buy.

Re:2d array file errors


On Thu, 17 Feb 2000 04:20:50 +0000, _meow

Quote
<b...@halligan74.junglelink.co.uk> wrote:
>hi all

>I have this problem. I am trying to write to a file useing a 2d array
>in a loop but I keep getting a type mismatch error
>the procedures are below.

>the type mismatch error I get when compiling is at the line
>write(datas,datastring[a1,b1]);
>in the save procedure

................. snip ................................
Sorry, I could not make out what you were doing but here is a simple
example of writing/reading to a text file and a file of bytes using 2D
arrays.
Clif

Program Array2dFiles;
CONST  source = 'test2d.txt';
       data = 'test2d.dat';
       prefix = 'This is array element A[';
VAR
f:Text;
fdat:File of Byte;
A:Array[1..4, 1..4] of String[80];
D:Array[1..20, 1..10] of Byte;
m, n:Byte;
s1, s2:String[1];

Begin
     For m := 1 to 4 Do
         For n := 1 to 4 Do
         Begin
              Str(m, s1);  Str(n, s2);
              A[m,n] := prefix + s1 + ',' + s2 + ']';  {make strings}
         End;

         Assign(f, source);
         Rewrite(f);
         For m := 1 to 4 Do
             For n := 1 to 4 Do Writeln(f, A[m,n]);

         For m := 1 to 4 Do
             For n := 1 to 4 Do A[m,n] := '';   {Erase array}

         Reset(f);
         For m := 1 to 4 Do
             For n := 1 to 4 Do Readln(f, A[m,n]);
         Close(f);

         For m := 1 to 4 Do
             For n := 1 to 4 Do Writeln(A[m, n]);
Writeln('PRESS ENTER':20); readln;

         For m := 1 to 20 Do
             For n := 1 to 10 Do D[m,n] := Random(200); {load data}

         Assign(fdat, data);
         Rewrite(fdat);
         For m := 1 to 20 Do
             For n := 1 to 10 Do
             Begin
                  Write(fdat, D[m,n]);
                  D[m,n] := 0; {erase data element}
             End;

         Reset(fdat);   {another way to read file for testing}
         m := 1;
         While not eof(fdat) Do
         Begin
              For n := 1 to 10 Do Read(fdat, D[m, n]);
              Inc(m);
         End;
         Close(fdat);

         For m := 1 to 20 Do
         Begin
              For n := 1 to 10 Do Write(D[m,n]:4);
              Writeln;
         End;

readln;
END.

Re:2d array file errors


Hi,

on Thu, 17 Feb 2000 at 04:20:50 o'clock, _meow wrote:

Quote
> the type mismatch error I get when compiling is at the line
> write(datas,datastring[a1,b1]);
> in the save procedure

This is a TYPE mismatch error. How are we supposed to answer your
question if you don't show us the declaration of datastring?

Quote
>    read(datas,datastring[a11,b11]);

> does this mean that a 2d array cannot be writen to file ?

No. BTW, datastring[a11,b11] is most probably not a 2d array (but I
can't say that for sure, because of the missing declaration).

Quote
> I have set the varables for both of the files the same type as the
> array ...

I doubt that. It should be an array[x_range,y_range] of String.

 - Sebastian

Other Threads