Solved

Reading and writing files

Posted on 1999-01-16
5
160 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 50 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

When we talk about DevOps toolchains, I sometimes wonder how many people really get what we’re talking about. I don’t know if it’s just semantics or tone or something else, but sometimes I think it just sounds like buzzword sausage. So it’s always …
Cloud-based technologies and services will continue to grow in popularity in 2017 thanks to the simple, scalable and cost-effective solutions they deliver. Here are three areas where cloud adoption is poised to really take off.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now