hi,

whenever i try to write records into the file, only the first piece of records can be written. even some simple program like the following cant work properly.

type thing=record
           name:string;
           end;      
var f:file of thing;
    data:array [1..2] of thing;
    i:integer;
begin
     assign(f, 'infile.txt');
     rewrite(f);
     data[1].name:='name';
     data[2].name:='second';
     for i:= 1 to 2 do
     write(f, data[i]);
     
     close(f);


end.

when i open 'infile.txt', only 'name' is shown but not 'name ' and 'second'. however, turbo-pascal works perfectly when array is written back into text file.

var f:text;
    data:array [ 1..2 ] of string;
    i:integer;
begin
     assign(f, 'infile.txt');
     rewrite(f);
     data[1]:='name';
     data[2]:='second';
     for i:=1 to 2 do
         writeln(f, data[i]);
     close(f);
end.

in this program, both 'name' and 'second' are shown. i am using both turbo-pascal and dev-pascal. is it the problem of my problem or the pascal software?????
thx for help!!!

grace

:D your first program working properly!I run your code and it is worked perfectly....(of course mine is Turbo Pascal 7.0 and dev pascal 1.9.2)
listen your code: :-/

for i:= 1 to 2 do write(f, data[i]);

there are two rounds so this code write two records to this file

maybe i should try the program in another computer XSS

I'm programming under windows xp

It is because you have misunderstood something.

A string is not a simple POD type -- it is an object type -- which you cannot write directly to file (according to Delphi standards [and also ISO standards, but that's beside the point ;) ]).

The write[[b]ln[/b]] functions know how to transform a string into a simple sequence of characters.

They do not know how to handle your thing type, because it has a non-POD member (the string).

When you create file types, they must be sequences of non-object types. Hence, type tFooFile: file of string; fails, where

type
  string80: array[ 1..80 ] of char;
  tFooFile: file of string80;

succeeds.

What you can do is write procedures/methods that know how to write your non-POD type to a given file, then call it when you wish to write it.

For more information, google around "delphi or pascal object serialization".

Hope this helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.