Solved

dynamically creating variable names??

Posted on 1998-11-13
7
223 Views
Last Modified: 2010-04-04
Here's a good one...I want to 'dynamically' create an unknown number of text output files based on a number (say between 1 and 100) the user inputs. If he inputs a 45, then I have to create 45 separate text output files on the fly. Is there any way to do this elegantly...or at all?
  Can a declaration like this be done? ...

outfile : array[1..100] of text;

...that would be perfect..then I could just go like this:

for x:=1 to {whatever # was input} do
    assignfile(outfile[x], 'file'+Inttostr(x)+'.txt');

...or am I just dreaming?

Cheers!
   Shawn Halfpenny

P.S: By the way, I'm using Delphi 3.
0
Comment
Question by:aztec
  • 4
  • 2
7 Comments
 
LVL 6

Accepted Solution

by:
Stuart_Johnson earned 50 total points
ID: 1346778
Shawn,

I just did this:

procedure TForm1.Button1Click(Sender: TObject);
Type FileInfo = Record
        TFile: TextFile;
        Active: Boolean;
     End;

var F: Array[1..100] of FileInfo;
    I: Integer;

begin
   For i := 1 to 100 do begin
      AssignFile(F[i].TFile, 'C:\'+IntTostr(I)+'.Txt');
      Rewrite(F[i].TFile);
      F[i].Active := True;
   End;
   For i := 1 to 100 do begin
      CloseFile(F[i].TFile);
      F[i].Active := False;
   End;
End;

And it worked perfectly!  The reason I did it as a record was so you could tell in an instant if they file was open or not.  This created and held open 100 files at once, then closed them.

Hope this helps you.

Regards,


Stu.
0
 
LVL 3

Expert Comment

by:bryan7
ID: 1346779
.. of course that you can..

just try it. sorry, I can't use delphi right now, but I'm sure you can
if you get an error like " incompatible.. string and PChar "
in the filename at Assignfile, then try this:

i.e: if the variable with files names is FILENAMES, then do this:
PChar(FILENAMES)    in stead of only         FILENAMES


bryan

0
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 1346780
Bryan,

I dont know if I follow, but I'll have a shot.  The AssignFile procedure has to be passed a string, not a PChar.  That is probably why you where getting the error.

Is that what you mean?  Sorry if its not.  Let me know if Im completely off track :)

Cheers!

Stu

0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 3

Expert Comment

by:bryan7
ID: 1346781
emm ,no.
I didn't remember if it needed string or PChar.
The thing is that I can't use delphi right now to check it.

bryan

0
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 1346782
No worries, just let me know when you get a chance.

Have fun!!

Stu
0
 

Author Comment

by:aztec
ID: 1346783
Right on...works fine! Thanks!
   Shawn
0
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 1346784
No worries mate, glad to be of service!!

Stu.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

757 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

20 Experts available now in Live!

Get 1:1 Help Now