Board index » delphi » Weird problem with 'Write'?

Weird problem with 'Write'?

Hello all,

I have a problem with the next program, the classic Eight Queens.
I run it in Borland Pascal 7, but that's not to off topic, I hope?

The problem is that the result of the first Write doesnt match the result
that's
actually written to the file 'bestand'.
The result on my screen counts 92 rows of numbers while the file contains 85
rows and 3 loose numbers. Weird!

Does anyone know why this happens?

program Queen;
uses WinCrt;
var
   h: array [1..8] of integer;
   k: integer;
   bestand: Text;

procedure zet;
var
   rk,i: integer;
   Toegelaten : Boolean;
begin
     for rk:=1 to 8 do
     begin
          Toegelaten :=True;
          i:=0;
          while Toegelaten and (i<k-1) do
          begin
               Inc(i);
               Toegelaten:=(h[i]<>rk) and (abs(h[i]-rk)<>k-i);
          end;
          if Toegelaten then
          begin
               h[k]:=rk;
               if k=8 then
               begin
                    for i:=1 to 8 do
                    begin
                        Write(h[i]:2);
                        Write(bestand,h[i]:2);
                    end;
                        Writeln;
                        Writeln(bestand);
               end else
               begin
                    Inc(k);
                    Zet;
               end;
          end;
          if rk=8 then Dec(k);
     end;
end;

begin
     Assign(bestand,'c:\windows\desktop\queens.txt');
     Rewrite(bestand);
     k:=1;
     Zet;
end.

 

Re:Weird problem with 'Write'?


When using a Text File, you have a Buffer that needs to get flushed..
 i see in your code that you are not closing the file or flushing it.
call Flush(bestand)  before leveing the local loop that writes the code..
  if i remember there is a 128 byte buffer that will cache data and will only
write it when the cache gets filled, this will speeds things up.
 you can force the cache to empty out to the disk file to insure all of the data
get
writen if your leving the file open..
Quote
Jesse van Oort wrote:
> Hello all,

> I have a problem with the next program, the classic Eight Queens.
> I run it in Borland Pascal 7, but that's not to off topic, I hope?

> The problem is that the result of the first Write doesnt match the result
> that's
> actually written to the file 'bestand'.
> The result on my screen counts 92 rows of numbers while the file contains 85
> rows and 3 loose numbers. Weird!

> Does anyone know why this happens?

> program Queen;
> uses WinCrt;
> var
>    h: array [1..8] of integer;
>    k: integer;
>    bestand: Text;

> procedure zet;
> var
>    rk,i: integer;
>    Toegelaten : Boolean;
> begin
>      for rk:=1 to 8 do
>      begin
>           Toegelaten :=True;
>           i:=0;
>           while Toegelaten and (i<k-1) do
>           begin
>                Inc(i);
>                Toegelaten:=(h[i]<>rk) and (abs(h[i]-rk)<>k-i);
>           end;
>           if Toegelaten then
>           begin
>                h[k]:=rk;
>                if k=8 then
>                begin
>                     for i:=1 to 8 do
>                     begin
>                         Write(h[i]:2);
>                         Write(bestand,h[i]:2);
>                     end;
>                         Writeln;
>                         Writeln(bestand);
>                end else
>                begin
>                     Inc(k);
>                     Zet;
>                end;
>           end;
>           if rk=8 then Dec(k);
>      end;
> end;

> begin
>      Assign(bestand,'c:\windows\desktop\queens.txt');
>      Rewrite(bestand);
>      k:=1;
>      Zet;
> end.

Other Threads