Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Reading and writing files

Posted on 1999-01-16
5
Medium Priority
?
173 Views
Last Modified: 2010-04-16
I have a file type declaration as follows.  

type
   CustomerData = record
      ACNumber : string[5];
      Name : string[20];
      Credit : integer;
   end;

   CustomerFile = file of CustomerData;

Now I want to create a file if it does not already exist and associate it with the file variable ( CustomerFile ) and I have problem with calling procedure assign  

assign(CustomerFile, 'C:\temp\hello.dat' );

It comes up with a compiler error saying that after first parameter a '(' is expected.

Where I am getting wrong?

If I replace CustomerFile with a text file declared as F: file, then it works, but I don't want a text file I want a typed file containing my customer records.

This is sort of a database - is there a better way to do it.  If I am on the right path then what is this error??

Can anybody give me code to open a file, write records to it close it, open it again and read the records and displayb them

Thanks in advance
0
Comment
Question by:xasan
  • 3
5 Comments
 
LVL 10

Accepted Solution

by:
viktornet earned 200 total points
ID: 1217495
Here is one example I wrote for you...

btw, The problem is that

CustomerFile = file of CustomerData;

is creating a data type and not a variable... In order for this to work you should do this...

var
  CustomerFile = file of CustomerData;

now when you do Assign(CustomerFile, 'C:\path\filename.xxx');

it will work....anyway, take a loook at the code below.. I wrote it so yoiu know how to read and  write records to a file... My example deletes the file at the end of the program but you could go without that :) Good Luck!!!

=============
uses crt;

const
  FileName = 'c:\windows\desktop\test.psw';

type
  TPassword = record
    Name        : string[20];
    Password : string[10];
  end;

procedure ReadType;
var
  F:FileofTPassword;
  MyPass:TPassword;
begin
  Assign(F, FileName);
  Reset(F);
  Read(F, MyPass);
  Close(F);
  WriteLn('Name = ', MyPass.Name);
  WriteLn('Password = ', MyPass.Password);
end;

procedure WriteType;
var
  F : File of TPassword;
  Pass : TPassword;
begin
  Assign(F, FileName);
  Rewrite(F);
  Write('Please enter your name ==> ');
  ReadLn(Pass.Name);
  Write('Please enter a password ==> ');
  ReadLn(Pass.Password);
  Write(F, Pass);
  Close(F);
end;

var
  tmp : File of TPassword;
begin
  clrscr;
  WriteType;
  ReadType;
  ReadKey;
  Assign(tmp, FileName);
  Erase(tmp);
end.
=============
Happy Coding!!!

-Viktor
--Ivanov
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 1217496
If you are using Delphi you should use the AssignFile function!
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1217497
Do you see the lines...

procedure ReadType;
   var
     F:FileofTPassword;
     MyPass:TPassword;

you should change those to

procedure ReadType;
   var
     F : File of TPassword;
     MyPass : TPassword;

so there are spaces... I had to go through all the spaces in order to clean the bad characters, but i seemed to forgot to correct these couple of lines.. Please correct them and then try compiling them... Thank you!

-Viktor
--Ivanov
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1217498
Epsylon, if he/she was using Delphi this sectiong wouldn't have been here... I answered his/her question why it didn't work... There is a separate area for Delphi... That's not the problem though :)

-Viktor
--Ivanov
0
 

Author Comment

by:xasan
ID: 1217499
Thank you Viktor

and yes, I am not using Delphi.  I am using Pascal
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Transferring FSMO roles is done when an admin wants to split roles between certain Domain Controllers or the Domain Controller holding the Roles has been forcefully demoted using dcpromo / forceremoval
There can be many situations demanding the conversion of Outlook OST files to PST format and as such, there is no shortage of automated tools to perform this conversion. However, what makes Stellar OST to PST converter stand above the rest? Let us e…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question