• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 421
  • Last Modified:

Creating Files Dynamically

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
aztec
Asked:
aztec
  • 4
  • 3
  • 3
1 Solution
 
viktornetCommented:
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
 
viktornetCommented:
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
 
aztecAuthor Commented:
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
Technology Partners: 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!

 
MadshiCommented:
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
 
aztecAuthor Commented:
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
 
MadshiCommented:
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
 
aztecAuthor Commented:
Madshi's reply worked super...could you please award him the points?...give him the "Excellent" category!

Cheers

P.S: Thanks anyway Viktor!!  :)
0
 
viktornetCommented:
You should award him the points :))

Madshi, answer the question :))
0
 
MadshiCommented:
Thanx to both of you...  :-)
0
 
aztecAuthor Commented:
Thank you Madshi!

Shawn
0

Featured Post

Industry Leaders: 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!

  • 4
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now