Solved

Reading and writing files

Posted on 1999-01-16
5
158 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
Comment Utility
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
Comment Utility
If you are using Delphi you should use the AssignFile function!
0
 
LVL 10

Expert Comment

by:viktornet
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you Viktor

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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
In this article, you will read about the trends across the human resources departments for the upcoming year. Some of them include improving employee experience, adopting new technologies, using HR software to its full extent, and integrating artifi…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 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

10 Experts available now in Live!

Get 1:1 Help Now