Solved

Creating Files Dynamically

Posted on 1999-01-16
10
408 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video discusses moving either the default database or any database to a new volume.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

747 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

13 Experts available now in Live!

Get 1:1 Help Now