943,901 Members | Top Members by Rank

Ad:
Aug 17th, 2009
0

why i cant write records into file in turbo-pascal???

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
amazing_grace is offline Offline
5 posts
since Aug 2009
Aug 17th, 2009
0

Re: why i cant write records into file in turbo-pascal???

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:
pascal Syntax (Toggle Plain Text)
  1. for i:= 1 to 2 do write(f, data[i]);
there are two rounds so this code write two records to this file
Reputation Points: 132
Solved Threads: 138
Posting Pro
FlamingClaw is offline Offline
559 posts
since Feb 2009
Aug 18th, 2009
0

Re: why i cant write records into file in turbo-pascal???

maybe i should try the program in another computer XSS
Reputation Points: 10
Solved Threads: 0
Newbie Poster
amazing_grace is offline Offline
5 posts
since Aug 2009
Aug 19th, 2009
0

Re: why i cant write records into file in turbo-pascal???

I'm programming under windows xp
Last edited by FlamingClaw; Aug 19th, 2009 at 4:26 am.
Reputation Points: 132
Solved Threads: 138
Posting Pro
FlamingClaw is offline Offline
559 posts
since Feb 2009
Aug 23rd, 2009
0

Re: why i cant write records into file in turbo-pascal???

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[ln] 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
Pascal Syntax (Toggle Plain Text)
  1. type
  2. string80: array[ 1..80 ] of char;
  3. 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.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Pascal and Delphi Forum Timeline: Count Lines Of Text In Txt File
Next Thread in Pascal and Delphi Forum Timeline: Could you mind helping me to code with the card game?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC