Solved

Creating Files Dynamically

Posted on 1999-01-16
10
412 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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
 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

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…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

840 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