?
Solved

dynamically creating variable names??

Posted on 1998-11-13
7
Medium Priority
?
230 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
[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
  • 4
  • 2
7 Comments
 
LVL 6

Accepted Solution

by:
Stuart_Johnson earned 200 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Suggested Courses
Course of the Month11 days, 7 hours left to enroll

752 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