?
Solved

Creating Files Dynamically

Posted on 1999-01-16
10
Medium Priority
?
417 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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!

 
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 200 total points
ID: 1362206
Thanx to both of you...  :-)
0
 

Author Comment

by:aztec
ID: 1362207
Thank you Madshi!

Shawn
0

Featured Post

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!

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month9 days, 13 hours left to enroll

762 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