Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

dynamically creating variable names??

Posted on 1998-11-13
7
Medium Priority
?
231 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 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
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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

971 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