hi,

i am working on a project to write data into the file and use a search enginee to get the data back. i was advised to use record but i didnt learn record at school. i tried to google it but came up with no solution. that's so far i got but there are a tons of errors:

type
data=record
name:string;
tel:string;
end;

var
infor:array[1..2] of data;
f,a:file of data;
begin

[here i assign some information to the record arrays]

assign(f,'file.txt');
rewrite(f);
writeln(f, infor[1]);
writeln(f, infor[2]);

[ i try to write the records into the file]
[ here is where i got the errors]


assign(a,'file.txt');
reset(a);
i:=1;
while not eof(a) do begin
readln(a, infor);
i:=i+1;
end;
writeln(infor[1].name);
writeln(infor[2].name);
[ i try to read the record back from file and display them on screen. but i never know if it works]


Please, i need your help!!! i am not sure if there is 'record array' and how to write or read' record array' into or out of the flie. please help me!!

grace

Recommended Answers

All 2 Replies

I'll try to explain everything...

Program Solution;

Uses Crt;

type data=record
            name:string;
            tel :string;
          end;
var
   infor:array[1..2] of data;
   F:file of data;
   i:Byte;{our little helper...}


Begin  {the main program}
     ClrScr;
     {
     here you assign the file's variable to the file.
     Remember that the 'file' word is reserved word!
     }
     Assign(F,'Yourfile.dat');
     {
     Ok.Now we want to create this file,or if it is exists then
     delete its contents!
     }
     Rewrite(F);
     {
     now we want to write to this file 2 records..
     let's see...
     }
     i:=1;
     WriteLn(i,'.Name: ');
     ReadLn(infor[1].name);{ok,we get the first field of the first record}
     WriteLn('Her/his phone: ');
     ReadLn(infor[1].tel);{and the phone too}
     Inc(i);
     WriteLn(i,'.Name: ');
     ReadLn(infor[2].name);{ and we get the first field of the second record}
     WriteLn('Her/his phone: ');
     ReadLn(infor[2].tel);{and the phone too}
     {
     ok,our array is filled by the user
     }
     WriteLn;{just an empty line...}

     {
     now we want to write this data to that file,assigned by F
     we use the 'for loop' to write the data,so we do not need
     to increase the value of 'i'.
     }
     For i:=1 To 2 Do{'cause our array has two elements(records)}
     Begin
        Write(F,infor[i]);{do you see???}
     End;{of for loop}
     {
     ok,now our array is written to the file!
     now we try to read it back
     }
     {
     but first we close this file..
     }
     Close(F);
     {
     opening for reading...
     }
     Reset(F);
     {
     we read the data back,and write to the screen...
     }
     i:=1;
     While Not (EoF(F)) Do
     Begin
        Read(F,infor[i]);
        Write(i,'.Name: ',infor[i].name,'  Tel: ',infor[i].tel);
        WriteLn;
        Inc(i);{same as i:=i+1}
     End;{of while}
     {
     and last,we close the file again...
     }
     Close(F);

     ReadLn;
End. {end of main program}

{Created in Turbo Pascal 7.0. by FlamingClaw 2009.08.14.}

:D

FlamingClaw's code is simply

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.