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

Reply

Join Date: Aug 2009
Posts: 5
Reputation: amazing_grace is an unknown quantity at this point 
Solved Threads: 0
amazing_grace amazing_grace is offline Offline
Newbie Poster

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

 
0
  #1
Aug 17th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 443
Reputation: FlamingClaw will become famous soon enough FlamingClaw will become famous soon enough 
Solved Threads: 106
FlamingClaw's Avatar
FlamingClaw FlamingClaw is offline Offline
Posting Pro in Training

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

 
0
  #2
Aug 17th, 2009
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:
  1. for i:= 1 to 2 do write(f, data[i]);
there are two rounds so this code write two records to this file
Be a good part of the community.Don't be ungrateful.
If you ask something on the forum and you got the right answer then mark as solved!
If my opinion helped to you a lot then sometimes give reputation point to me.
I'm just a pascal programmer from Hungary.
Farewell...
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 5
Reputation: amazing_grace is an unknown quantity at this point 
Solved Threads: 0
amazing_grace amazing_grace is offline Offline
Newbie Poster

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

 
0
  #3
Aug 18th, 2009
maybe i should try the program in another computer XSS
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 443
Reputation: FlamingClaw will become famous soon enough FlamingClaw will become famous soon enough 
Solved Threads: 106
FlamingClaw's Avatar
FlamingClaw FlamingClaw is offline Offline
Posting Pro in Training

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

 
0
  #4
Aug 19th, 2009
I'm programming under windows xp
Last edited by FlamingClaw; Aug 19th, 2009 at 4:26 am.
Be a good part of the community.Don't be ungrateful.
If you ask something on the forum and you got the right answer then mark as solved!
If my opinion helped to you a lot then sometimes give reputation point to me.
I'm just a pascal programmer from Hungary.
Farewell...
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

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

 
0
  #5
Aug 23rd, 2009
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
  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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC