| | |
why i cant write records into file in turbo-pascal???
![]() |
•
•
Join Date: Aug 2009
Posts: 5
Reputation:
Solved Threads: 0
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.
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.
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
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
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)
for i:= 1 to 2 do write(f, data[i]);
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...
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...
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...
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...
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,
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.
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)
type string80: array[ 1..80 ] of char; tFooFile: file of string80;
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.
![]() |
Similar Threads
- how to write and read array record into file?? (Pascal and Delphi)
- Error 207:Invalid floating point operation??? in Turbo PASCAL (Pascal and Delphi)
- Turbo pascal Homework.. (Pascal and Delphi)
- Help with linked list in Turbo Pascal! (Pascal and Delphi)
- Error trying to write to existing file (C)
- help needed to print from turbo pascal (Pascal and Delphi)
- Turbo pascal homework (Pascal and Delphi)
- How to write a header file (C++)
Other Threads in the Pascal and Delphi Forum
- Previous Thread: Count Lines Of Text In Txt File
- Next Thread: Could you mind helping me to code with the card game?
| Thread Tools | Search this Thread |






