Say I have a file of string[20] . I can easily write to this, but as soon as I try to receive the data I get incompatibility errors. So I make a string[20] variable to see if I can read into it. I cannot. It just says incompatible types. When the [20] is removed it says incompatible types string and shortstring. Any ideas of how to fix such a problem?

program solution28;

uses crt,dos;

type newtype = string[20];

var f:file of newtype;
   

procedure game_over(s:string);
begin
     writeln(s);
     writeln('press enter to quit.');
     readln;
     halt;
end;

procedure write_in;
var x:newtype;
begin
     assign(f,'new.dat');
     {$I-}
     rewrite(f);
     {$I+}
     if ioresult <> 0 then game_over('file rewrite error');
     write('give me your string: ');
     readln(x);
     write(f,x);
     close(f);
end;

procedure read_out;
var x:newtype;
begin
    assign(f,'new.dat');
    {$I-}
    reset(f);
    {$I+}
    if ioresult <> 0 then game_over('file reset error');
    writeln('the file''s contets are: ');
    while not eof(f) do begin
          read(f,x);
          writeln(x);
    end;
    close(f);
end;

begin (*main*)
   clrscr;
   write_in;
   read_out;
   repeat
   until keypressed;
end. (*main*)
(*created by FlamingClaw 2010.02.18. Hungary *)

Edited 6 Years Ago by FlamingClaw: n/a

program write_file_read_out;

{$APPTYPE CONSOLE}

uses
  SysUtils;


type newtype = string[20];

var f:file of newtype;


procedure game_over(s:string);
begin
     writeln(s);
     writeln('press enter to quit.');
     readln;
     halt;
end;

procedure write_in;
var x:newtype;
begin
     assign(f,'new.dat');
     {$I-}
     rewrite(f);
     {$I+}
     if ioresult <> 0 then game_over('file rewrite error');
     write('give me your string: ');
     readln(x);
     write(f,x);
     close(f);
end;

procedure read_out;
var x:newtype;
begin
    assign(f,'new.dat');
    {$I-}
    reset(f);
    {$I+}
    if ioresult <> 0 then game_over('file reset error');
    writeln('the file''s contets are: ');
    while not eof(f) do begin
          read(f,x);
          writeln(x);
    end;
    close(f);
end;

begin (*main*)
   write_in;
   read_out;
   readln;
end. (*main*)
(*created by FlamingClaw 2010.02.18. Hungary *)

this is the delphi 7 version.... :D (cause here there is not crt unit... :'( )

Thanks, I wandered why this wasnt working, I always thought types where for if you wanted more than one variable type incoperated into one file. I guess im wrong. Thanks again.

This question has already been answered. Start a new discussion instead.