Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Reading and writing files

Posted on 1999-01-16
5
Medium Priority
?
172 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 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Read on to get a few ideas on how to promote your next corporate event.
What we learned in Webroot's webinar on multi-vector protection.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

670 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