Solved

dynamically creating variable names??

Posted on 1998-11-13
7
228 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

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

Suggested Solutions

Title # Comments Views Activity
Delphi 2 87
TEMBEDDEDWB how can i change its user agent ? 8 99
Base1 Encode/Decode 3 96
How to build JSON File in Delphi 6 3 85
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…

740 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