Solved

Creating Files Dynamically

Posted on 1999-01-16
10
409 Views
Last Modified: 2008-02-01
Hi, I'm using Delphi 3 and my app has a section where it opens and creates many files dynamically depending on a number the user inputs. In other words it opens up a whole bunch of text files simultaneously...here's the code:

for x:=1 to num do
begin
  assignfile(specfile[x], 'file'+inttostr(num)+'.txt')
  rewrite(specfile[x]);
end;

...specfile is declared as:
specfile : Array[1..1000] of text;

I'd like to give it an upper limit of about 1000 simultaneous open files, but it looks like it's conking out after about 140 or so. It gives an Exception Class ElnOutError with message I/O Error 32. Am I exceeding some sort of maximum open files limit here? Is there a way to increase it?

Thanks
   Shawn Halfpenny
   drumme59@sprint.ca
0
Comment
Question by:aztec
  • 4
  • 3
  • 3
10 Comments
 
LVL 10

Expert Comment

by:viktornet
ID: 1362198
Try this,... I have not tested it so if you are still having problems i'll test it and let you know how to do it.. for now try this....

The example shows you how to do that for 100 and you can do as many as you want as long as you have space, .// but you are not writing anything to them so they are zero bytes....

var
  f : TextFile;
  i : integer;
begin
  for i := 1 to 100 do begin
    AssignFile(f, 'file' + IntToStr(i) + '.txt');
    rewrite(f);
    CloseFile(f);
  end;
end;

Tell me how this works, ok? I'll go test it and let you know if it works fine or needs more coding...

-Viktor
--Ivanov
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1362199
OK, everything works smoothly,,, Here is what I tried and it works fine :))

procedure TForm1.Button1Click(Sender: TObject);
const
      path : string = 'C:\windows\desktop\test\';
var
      i : integer;
      f : TextFile;
begin
      for i := 0 to 1000 do begin
            AssignFile(f, path + 'file'+IntToStr(i) + '.txt');
            ReWrite(f);
            CloseFile(f);
      end;
end;

The problem is that you were not closing the files, and there were too many in memory probably...

-Viktor
--Ivanov
0
 

Author Comment

by:aztec
ID: 1362200
Hello Viktor...
   Thanks for your response. But the thing is, I *want* to keep all 1000 files open at the same time...I guess that is impossible, hmm? What I am trying to do, is read a file and depending on the type of record I read, I write it to any one of these 1000 open files. I guess I can still do what I want to do, but I just have to re-open, write to, then re-close the file in question everytime, yes?

Cheers
    Shawn
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1362201
Hi girls...

Try this. It runs perfectly. Error 32 means that the process can't access this file because this file is in use by another process. However, it works fine with TFileStream files...

procedure testFiles;
var fsArray : array [0..999] of TFileStream;
    i1      : integer;
begin
  for i1:=0 to high(fsArray) do
    fsArray[i1]:=TFileStream.Create('c:\test\test'+IntToStr(i1),fmCreate);
  for i1:=0 to high(fsArray) do
    fsArray[i1].Free;
end;

Regards, Madshi.
0
 

Author Comment

by:aztec
ID: 1362202
Hello Viktor/Madshi...
   Viktor, I tried your sueggestion but to open and close files so often really slows down my app..I cannot do it that way. Madshi, I will have to try your suggestion regarding TFileStream, but I have never used streams before. How do I write a regular text record (ending in a carriage return/line feed) to a FileStream? Also, when I am done the writing process, will I be able to close these files and read them as regular text files? (ie. using AssignFile and regular READLN procedure).

Thanks!
   Shawn Halfpenny

P.S: Madshi - so using TFileStream I can *really* keep 1000 files open simultaneously? Maybe 2000?  :-)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 20

Expert Comment

by:Madshi
ID: 1362203
Shawn,

if you have a complete text in a string (with carriage return/line feed in it (#$D#$A)) you can simply do the following and - YES - you can read the files as regular text files afterwards.

var s1 : string;
    fs : TFileStream;
begin
  fs:=TFileStream.Create('c:\test.txt',fmCreate);
  try
    fs.Write(pchar(s1)^,length(s1));
  finally fs.Free end;
end;

That was it!!!

I tested only 1000 files, but I'm quite sure that you can use more than that. However, each and every file uses system resources. Please look how they decrease while you open so many files (right click on My Computer, select properties, look at the last tab). So it will be restricted somehow...   :-(

Regards, Madshi.
0
 

Author Comment

by:aztec
ID: 1362204
Madshi's reply worked super...could you please award him the points?...give him the "Excellent" category!

Cheers

P.S: Thanks anyway Viktor!!  :)
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1362205
You should award him the points :))

Madshi, answer the question :))
0
 
LVL 20

Accepted Solution

by:
Madshi earned 50 total points
ID: 1362206
Thanx to both of you...  :-)
0
 

Author Comment

by:aztec
ID: 1362207
Thank you Madshi!

Shawn
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi XE10 Round Image 2 79
Delphi 2 51
Open a URL with Internet Explorer in a new tab (not a new window) 1 72
Unique identifier on a terminal server (rdp) 4 46
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 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

23 Experts available now in Live!

Get 1:1 Help Now