System.Write broken?

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

System.Write broken?

Bugzilla from elio@mixtk.com
Hi, the folowing code enters infinite loop in the newest version of
FreePascal:

program textfile;

var
  Archivo: Text;
  Linea: AnsiString;

begin
  Assign(Archivo, 'test.pp');
  Reset(Archivo);
  While not Eof(Archivo) do
  begin
    Read(Archivo, Linea);
    WriteLn(Linea);
  end;
  Close(Archivo);
end.

Am i doing something wrong?
This code worked in older releases of the compiler but loops forever in this
version. Thanks.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: System.Write broken?

Micha Nelissen
On Wed, 13 Jul 2005 16:02:21 -0500
Elio Cuevas Gómez <[hidden email]> wrote:

> Hi, the folowing code enters infinite loop in the newest version of
> FreePascal:
>
> program textfile;
>
> var
>   Archivo: Text;
>   Linea: AnsiString;
>
> begin
>   Assign(Archivo, 'test.pp');
>   Reset(Archivo);
>   While not Eof(Archivo) do
>   begin
>     Read(Archivo, Linea);

Don't you want 'ReadLn' ?

What does Read do anyway, on PChars, strings and arrays ?

Micha

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: System.Write broken?

L505
>Don't you want 'ReadLn' ?
>What does Read do anyway


Read character by character instead of line by line. Both useful for different
things, read is more precise.
Say if your file was not delimted by lines. (pipes, commas, etc.)
Readln() is essentially a wrapper for Read()
Read finds CRLF's or LF's and reports back to the readln function.

hope i'm not patronizing



_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: System.Write broken?

Bugzilla from elio@mixtk.com
In reply to this post by Micha Nelissen
El Mié 13 Jul 2005 16:07, Micha Nelissen escribió:
>
> Don't you want 'ReadLn' ?
>
> What does Read do anyway, on PChars, strings and arrays ?
>
> Micha
>
Ok, i gave a bad example (and ReadLn works for that) but in my app i read a
tile map from a text file. The format is very simple: The values are unsigned
integers separated with spaces or newlines, for example:
0 0 1 0 0 0
0 0 1 0 2 2
0 0 1 0 2 2
0 3 3 3 3 2
0 3 3 0 3 3
0 0 0 1 1 1
The code to read the file looks like this (yeah, bad programming i know):
for j := 0 to MAX_MAPY do
    for i := 0 to MAX_MAPX do
      Read(MapFile, Mapa[i, j]);

The point is that this worked before, but now loops forever (which is odd
since is a 32x32 for, looks like the problem is in Read). I tried to use
ReadLn instead, it doesn't hang but then i get only zeroes.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: System.Write broken?

John Coppens
On Wed, 13 Jul 2005 16:50:03 -0500
Elio Cuevas Gómez <[hidden email]> wrote:


> The code to read the file looks like this (yeah, bad programming i
> know): for j := 0 to MAX_MAPY do
>     for i := 0 to MAX_MAPX do
>       Read(MapFile, Mapa[i, j]);
>

How is Mapa declared? If as integers, it should work. This does not work
with strings.

It'd be a little more elegant if you do:

for j := 0 to MAX_MAPY do begin
  for i := 0 to MAX_MAPX do
    Read(MapFile, Mapa[i, j]);
  ReadLn(MapFile);
end;

garanteeing you read MAX_MAPY lines

John

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: System.Write broken?

Bugzilla from elio@mixtk.com
El Mié 13 Jul 2005 17:50, John Coppens escribió:
>
> How is Mapa declared? If as integers, it should work. This does not work
> with strings.
var
  MapFile: Text;
  Mapa: array[0..MAX_MAPX, 0..MAX_MAPY] of Integer;

Yes, it's as integers.

>
> It'd be a little more elegant if you do:
>
> for j := 0 to MAX_MAPY do begin
>   for i := 0 to MAX_MAPX do
>     Read(MapFile, Mapa[i, j]);
>   ReadLn(MapFile);
> end;
>
> garanteeing you read MAX_MAPY lines
>
I tried your version, but didn't make much diference. Looks like Write is
indeed broken, i'll report it as a bug. Thanks for all your help :).

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: System.Write broken?

John Coppens
On Wed, 13 Jul 2005 19:55:33 -0500
Elio Cuevas Gómez <[hidden email]> wrote:


> I tried your version, but didn't make much diference. Looks like Write
> is indeed broken, i'll report it as a bug. Thanks for all your help :).

I tried it here - works fine for me (See code tested below). A doubt: did
you correctly set the for loops... 0..MAX_MAPY is actually one more that
you might expect! As I cannot see the definition of MAX_MAPY, I don't
know if they're correct.

Anyways, this works (note 1..5 in array def, and loops):

program test;

var
  Map: array [1..6, 1..6] of integer;
  i, j: integer;
  MapFile: text;

begin
  assign(MapFile, 'test.map');
  reset(MapFile);
  for j := 1 to 6 do begin
    for i := 1 to 6 do    
      Read(MapFile, Map[i, j]);
  end;
  for j := 1 to 6 do begin
    for i := 1 to 6 do    
      write(Map[i, j]:3);
    writeln;            
  end;
  close(MapFile);
end.


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: System.Write broken?

Tomas Hajny
On Thu, July 14, 2005 3:55, John Coppens said:

> On Wed, 13 Jul 2005 19:55:33 -0500
> Elio Cuevas G?mez <[hidden email]> wrote:
>
>
>> I tried your version, but didn't make much diference. Looks like Write
>> is indeed broken, i'll report it as a bug. Thanks for all your help :).
>
> I tried it here - works fine for me (See code tested below). A doubt: did
> you correctly set the for loops... 0..MAX_MAPY is actually one more that
> you might expect! As I cannot see the definition of MAX_MAPY, I don't
> know if they're correct.
 .
 .

If your problem appears with version 2.0.0, then it's most probably a bug
already reported and fixed in the meantime (in SVN/snapshots). The problem
only appears if the last line containing your data isn't finished with
line end.

Regards

Tomas


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: System.Write broken?

Bugzilla from elio@mixtk.com
El Jue 14 Jul 2005 02:33, Tomas Hajny escribió:

>
> If your problem appears with version 2.0.0, then it's most probably a bug
> already reported and fixed in the meantime (in SVN/snapshots). The problem
> only appears if the last line containing your data isn't finished with
> line end.
>
> Regards
>
> Tomas
>
That's why i didn't report it. I added the ending line to the data file and it
works for now. Thanks a lot!
>
> _______________________________________________
> fpc-pascal maillist  -  [hidden email]
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal