473,397 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Open File

Hi
I am a student doing a project using C++ builder. I hava a problem in
opening a number of files more than 47, when run it only it create 46
file, Please help me to be able to create any number of files. Thanks
to your efforts.
Mohd Klaib
Example :

FILE *out[100];
char filename[100];
int noFiles=78;

for (int i=0 ; i<noFiles ; i++)
{
sprintf (filename,"c:/test/TestSuite%d",i);
out[i]= fopen(filename, "wt");
}

May 28 '07 #1
13 4836
mo*******@gmail.com wrote:
Hi
I am a student doing a project using C++ builder. I hava a problem in
opening a number of files more than 47, when run it only it create 46
file, Please help me to be able to create any number of files. Thanks
to your efforts.
Mohd Klaib
Example :

FILE *out[100];
char filename[100];
int noFiles=78;

for (int i=0 ; i<noFiles ; i++)
{
sprintf (filename,"c:/test/TestSuite%d",i);
out[i]= fopen(filename, "wt");
}
This looks more like a C problem.

There isn't a lot you can do if you have hit the number of open files
limit for your system. You would be better off asking on a platform
specific group.

--
Ian Collins.
May 28 '07 #2
mo*******@gmail.com wrote:
Hi
I am a student doing a project using C++ builder. I hava a problem in
opening a number of files more than 47, when run it only it create 46
file, Please help me to be able to create any number of files. Thanks
to your efforts.
Mohd Klaib
Example :

FILE *out[100];
char filename[100];
int noFiles=78;

for (int i=0 ; i<noFiles ; i++)
{
sprintf (filename,"c:/test/TestSuite%d",i);
out[i]= fopen(filename, "wt");
}
Do you really have to have 78 files open *simultaneously*? That's hard
to believe. I suggest you operate on the files one at a time, and close
them when you are finished. Reorganise your code to do this.

john
May 28 '07 #3
On May 28, 9:00 am, John Harrison <john_androni...@hotmail.comwrote:
mom.kl...@gmail.com wrote:
I am a student doing a project using C++ builder. I hava a problem in
opening a number of files more than 47, when run it only it create 46
file, Please help me to be able to create any number of files. Thanks
to your efforts.
Example :
FILE *out[100];
char filename[100];
int noFiles=78;
for (int i=0 ; i<noFiles ; i++)
{
sprintf (filename,"c:/test/TestSuite%d",i);
out[i]= fopen(filename, "wt");
}
Do you really have to have 78 files open *simultaneously*? That's hard
to believe.
For a student project, maybe, but there's nothing particularly
extraordinary for a server to have several hundred files open
simultaneously.
I suggest you operate on the files one at a time, and close
them when you are finished.
That would mean opening the files for each request, and closing
it at the end of the request. Which would slow things down
considerably, and wouldn't necessarily help, since different
threads (different client connections) will still access
different files.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 28 '07 #4
On 5ÔÂ28ÈÕ, ÏÂÎç2ʱ07·Ö, mom.kl...@gmail.com wrote:
Hi
I am a student doing a project using C++ builder. I hava a problem in
opening a number of files more than 47, when run it only it create 46
file, Please help me to be able to create any number of files. Thanks
to your efforts.
Mohd Klaib
Example :

FILE *out[100];
char filename[100];
int noFiles=78;

for (int i=0 ; i<noFiles ; i++)
{
sprintf (filename,"c:/test/TestSuite%d",i);
out[i]= fopen(filename, "wt");
}
Maybe you can use Windows API e.g. OpenFile(...). But I don't know how
much files can be opened simultaneously by this API.

May 28 '07 #5
On 5ÔÂ28ÈÕ, ÏÂÎç8ʱ01·Ö, kingfox <foxap...@gmail.comwrote:
On 5ÔÂ28ÈÕ, ÏÂÎç2ʱ07·Ö, mom.kl...@gmail.com wrote:


Hi
I am a student doing a project using C++ builder. I hava a problem in
opening a number of files more than 47, when run it only it create 46
file, Please help me to be able to create any number of files. Thanks
to your efforts.
Mohd Klaib
Example :
FILE *out[100];
char filename[100];
int noFiles=78;
for (int i=0 ; i<noFiles ; i++)
{
sprintf (filename,"c:/test/TestSuite%d",i);
out[i]= fopen(filename, "wt");
}

Maybe you can use Windows API e.g. OpenFile(...). But I don't know how
much files can be opened simultaneously by this API.- Òþ²Ø±»ÒýÓÃÎÄ×Ö -

- ÏÔʾÒýÓõÄÎÄ×Ö -
I just wrote a program to test how many files can be opened by Windows
API - OpenFile. This program open more than 5000 files simultaneously.
So I think OpenFile can fit your request. The program as below:

//--------------------------------------------------------------------------------------------------
#include <iostream>
#include <vector>
#include <sstream>
using namespace std;
#include <windows.h>

void closeFilesByAPI(HANDLE fileHandle)
{
CloseHandle(fileHandle);
}

void howManyFilesCanBeOpenedByAPI()
{
vector<HANDLEhandleVector;
HANDLE fileHandle;
int count = 0;
DWORD writtenBytes;
do {
stringstream fileName;
fileName.clear();
fileName << "file" << count;
fileHandle = CreateFile(fileName.str().c_str(), GENERIC_WRITE,
FILE_SHARE_WRITE, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL
+FILE_FLAG_RANDOM_ACCESS, NULL);
if (fileHandle == INVALID_HANDLE_VALUE)
break;
handleVector.push_back(fileHandle);
WriteFile(fileHandle, fileName.str().c_str(),
fileName.str().size(), &writtenBytes, NULL);
cout << fileName.str() << '\r';
count ++;
}while(fileHandle != INVALID_HANDLE_VALUE);
cout << "Total number of files opened by ofstream is: " << count <<
endl;
for_each(handleVector.begin(), handleVector.end(),
closeFilesByAPI);
}
//--------------------------------------------------------------------------------------------------

May 28 '07 #6
mo*******@gmail.com a écrit :
Hi
I am a student doing a project using C++ builder. I hava a problem in
opening a number of files more than 47, when run it only it create 46
file, Please help me to be able to create any number of files. Thanks
to your efforts.
You can have a look at the _NFILE defines in stdio.
It should give you the maximum number of file you can open with fopen.

Try to display errno and associated error with perror upon open failure.
It can help you identify the problem.

In your program:

sprintf (filename,"c:/test/TestSuite%d",i);
out[i]= fopen(filename, "wt");
if(out[i]<=0)
{//error
perror(filename);
}

Michael
May 28 '07 #7
In article <R5*****************@newsfe7-gui.ntli.net>,
jo*************@hotmail.com says...

[ ... ]
Do you really have to have 78 files open *simultaneously*? That's hard
to believe. I suggest you operate on the files one at a time, and close
them when you are finished. Reorganise your code to do this.
On many systems, opening or closing a file is a fairly expensive
operation, so this is likely to have a significant performance penalty.

It's technically off-topic, but most standard libraries I've looked at
had this limit set in a fairly easy-to-find definition. Adjust the
definition, re-compile the library, and you're off and running.

As long as we're off-topic: elsethread OpenFile was mentioned. MS
considers this obsolete, and new code is encouraged to use CreateFile
instead (though OpenFile probably isn't any more likely to disappear any
time soon than parts of C++ that have been deprecated...)

--
Later,
Jerry.

The universe is a figment of its own imagination.
May 28 '07 #8
On Mon, 28 May 2007 18:24:47 +1200, Ian Collins <ia******@hotmail.com>
wrote in comp.lang.c++:
mo*******@gmail.com wrote:
Hi
I am a student doing a project using C++ builder. I hava a problem in
opening a number of files more than 47, when run it only it create 46
file, Please help me to be able to create any number of files. Thanks
to your efforts.
Mohd Klaib
Example :

FILE *out[100];
char filename[100];
int noFiles=78;

for (int i=0 ; i<noFiles ; i++)
{
sprintf (filename,"c:/test/TestSuite%d",i);
out[i]= fopen(filename, "wt");
}
This looks more like a C problem.
Looks like an obstructionist problem.

Wait, let me double check...

Yes, both sprintf() and fopen() are defined in the latest version of
the C++ standard.

In fact with addition of:

#include <cstdio>
#include <cstring>
using namespace std;

....and placement inside a suitable function, the user's snippet is
100% perfectly conforming standard C++ code.

Even with the addition of:

#include <stdio.h>
#include <string.h>

....the code is conforming, although deprecated.
There isn't a lot you can do if you have hit the number of open files
limit for your system. You would be better off asking on a platform
specific group.
Your advice is quite correct, but your comment is completely
incorrect. If you don't like using standard C++ library functions
that happen to be inherited from C, that's fine and dandy.

But no use of a function defined by the C++ standard is a "C problem".

Believe it or not, the C standard says absolutely nothing at all about
the behavior of fopen() or sprintf() in a C++ program. Anything and
everything in a C++ program is off-topic in C language groups.

The fact that C++ defines some functions with the same names as those
in the C library, and defines them to accept similar parameters and
return similar values, does not make them C functions when used in a
C++ program.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
May 29 '07 #9
On Mon, 28 May 2007 18:56:38 -0500, Jack Klein wrote:
Even with the addition of:

#include <stdio.h>
#include <string.h>

...the code is conforming, although deprecated.
Why should this be conforming? These are implementation specific header
files that need not work with anything but the specific C compiler they
are part of.

Of course in practice many vendors offer C and C++ compilers and design
there C system header files in such a way that they are compatible with
their C++ compiler but that is certainly not a requirement of either the
C or C++ standard.

--
Markus Schoder
May 29 '07 #10
On May 29, 10:02 am, Markus Schoder <a3vr6dsg-use...@yahoo.dewrote:
On Mon, 28 May 2007 18:56:38 -0500, Jack Klein wrote:
Even with the addition of:
#include <stdio.h>
#include <string.h>
...the code is conforming, although deprecated.

Why should this be conforming? These are implementation specific header
files that need not work with anything but the specific C compiler they
are part of.

Of course in practice many vendors offer C and C++ compilers and design
there C system header files in such a way that they are compatible with
their C++ compiler but that is certainly not a requirement of either the
C or C++ standard.

--
Markus Schoder
Thank you for your advises and help.
klaib

May 29 '07 #11
Jack Klein wrote:
On Mon, 28 May 2007 18:24:47 +1200, Ian Collins <ia******@hotmail.com>
wrote in comp.lang.c++:
[...]
Your advice is quite correct, but your comment is completely
incorrect. If you don't like using standard C++ library functions
that happen to be inherited from C, that's fine and dandy.
The original code was bad C++, even if it is legal. (Goto is
legal C++. Even gets() is legal C++. That doesn't mean that
they are good.)
But no use of a function defined by the C++ standard is a "C problem".
Believe it or not, the C standard says absolutely nothing at all about
the behavior of fopen() or sprintf() in a C++ program.
It most certainly does. The definition of these functions in
the C++ standard is: "see ISO 9899".
Anything and
everything in a C++ program is off-topic in C language groups.
The fact that C++ defines some functions with the same names as those
in the C library, and defines them to accept similar parameters and
return similar values, does not make them C functions when used in a
C++ program.
The fact that a C++ compiler can sometimes compiler pure C
doesn't make a program in pure C++ C++. It's not clear whether
the original poster was trying to write C or C++. If he's
trying to write C++, he's going about it wrong, as there are far
better solutions in C++.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 29 '07 #12
On May 29, 4:02 am, Markus Schoder <a3vr6dsg-use...@yahoo.dewrote:
On Mon, 28 May 2007 18:56:38 -0500, Jack Klein wrote:
Even with the addition of:
#include <stdio.h>
#include <string.h>
...the code is conforming, although deprecated.
Why should this be conforming?
Because the C++ standard requires it. The C++ standard also
requires support for "C" language linkage, which more or less
supposes that the C++ compiler "knows" about at least one C
compiler on the target platform. (The C++ standard doesn't say
how an implementation is supposed to treat this if there is no C
compiler for the target platform. To my knowledge, however,
this problem has yet to occur.)
These are implementation specific header files that need not
work with anything but the specific C compiler they are part
of.
Of course in practice many vendors offer C and C++ compilers and design
there C system header files in such a way that they are compatible with
their C++ compiler but that is certainly not a requirement of either the
C or C++ standard.
There is a a requirement in the C++ standard that the headers
stdio.h and string.h exist in the C++ implementation, and that
they have very specific semantics, which are subtly different
than those of C. In practice, most implementations do use the
same headers (with a few #ifdef's) in both C and C++, although
this normally results in the headers not being fully conform to
the C++ standard.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 29 '07 #13
Jack Klein wrote:
On Mon, 28 May 2007 18:24:47 +1200, Ian Collins <ia******@hotmail.com>
wrote in comp.lang.c++:
>mo*******@gmail.com wrote:
>>Hi
I am a student doing a project using C++ builder. I hava a problem in
opening a number of files more than 47, when run it only it create 46
file, Please help me to be able to create any number of files. Thanks
to your efforts.
Mohd Klaib
Example :

FILE *out[100];
char filename[100];
int noFiles=78;

for (int i=0 ; i<noFiles ; i++)
{
sprintf (filename,"c:/test/TestSuite%d",i);
out[i]= fopen(filename, "wt");
}
This looks more like a C problem.

Looks like an obstructionist problem.

Wait, let me double check...
<rant snipped>

Was all that nonsense really worth the effort expended typing it?

--
Ian Collins.
May 29 '07 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Stephen | last post by:
could someone give me some advise on a problem I have. At present i have a web application which displays pdf files in a web browser using javascript and the window.open method and pointing to a...
2
by: gary smith | last post by:
I am trying to open a file (file --> open --> file) at an FTP location using visual Studio.net. Unfortunately, I get an "unspecified error" alert whenever I select a file. Can anyone help...
3
by: Murasama | last post by:
Hi there, Im trying a simple file IO operation in Visual Studio .NET 2003 and it can't seem to open the file. If I run the exe in the debug directory it works fine but if I click the start...
6
by: Charles Morrall | last post by:
I have no experience with DB2 as such, but I've been tasked with configuring backup of a server running DB2 v8 on Windows Server 2003. I do have some experience with backups in general though. The...
2
by: nissiml | last post by:
hi, i'm trying to open a asp.net web page that list files from a Windows application like winword and select a file from it . what do i have to do to make it happen, is it simple ? Thanks in...
2
by: Mattbooty | last post by:
Hello, Not sure if anyone else has seen this bug, but I have a form where the entire form is covered with a picturebox. The picturebox has a mouseup event. I also have an open file dialog for...
2
by: agphoto | last post by:
There is big or problem in open file in read and write mode.. $file = "data.txt"; $fp = fopen($file,"w+"); $line = fgets($fp,"120"); // i need only 1st line to read and upto 120 bytes echo...
4
by: DyslexicAnaboko | last post by:
Hello, I have a module that is part of larger project that is giving me trouble, so I setup an example. Brief ===== I simply want to open a text file and make the contents avaliable...
5
by: Ryan Liu | last post by:
Hi, Both way works, I'd just ask some experts which way is better? My application creates a log file daily. Now each time when I write a log, I will open the file and append to the end....
18
by: Coffee Pot | last post by:
Thanks for any advice. ~ CP
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.