Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2005-07

System.Write broken?

9 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Elio Cuevas Gómez

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.

Reply by email

Micha Nelissen
to Elio Cuevas Gómez
Show quoted text 15 lines
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

Reply by email

Elio Cuevas Gómez
to Micha Nelissen
Micha Nelissen wrote 7 lines
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.

Reply by email

L505
to Micha Nelissen: On Wed, 13 Jul 2005 16:02:21 -0500 Don't you want 'ReadLn' ? What does Read do anyway, on…
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

Reply by email

John Coppens
to Elio Cuevas Gómez: Ok, i gave a bad example (and ReadLn works for that) but in my app i read a tile map from…
Show quoted text 5 lines
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

Reply by email

Elio Cuevas Gómez
to John Coppens
John Coppens wrote
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.

Show quoted text 11 lines
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 :).

Reply by email

John Coppens
to Elio Cuevas Gómez
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.

Reply by email

Tomas Hajny
to John Coppens

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

Show quoted text 11 lines
On Wed, 13 Jul 2005 19:55:33 -0500 Elio Cuevas Gómez 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

Reply by email

Elio Cuevas Gómez
to Tomas Hajny
Tomas Hajny wrote 10 lines
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 - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email