Solved

dynamically creating variable names??

Posted on 1998-11-13
7
229 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 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
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 …

688 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