Solved

Reading and writing files

Posted on 1999-01-16
5
167 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Getting to know the threat landscape in which DDoS has evolved, and making the right choice to get ourselves geared up to defend against  DDoS attacks effectively. Get the necessary preparation works done and focus on Doing the First Things Right.
Microsoft Office 365 is a subscriptions based service which includes services like Exchange Online and Skype for business Online. These services integrate with Microsoft's online version of Active Directory called Azure Active Directory.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

630 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