473,739 Members | 3,096 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"To many files open error <EMFILE>" bug

I'm making an program that encodes text, and then writes it into
another text file. The problem is that I can't seem to get the "file
exists" checking right. If I use the command (coder is the
application):
coder something.txt an_exsiting_fil e.txt, then if I chose to overwrite
"an_exsiting_fi le.txt", an error message pops up, saying that to many
files are open <EMFILE>. Any help would be appreciated. Here is the
code (incomplete but still compilable):

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

int main(int argc, char *argv[])
{
FILE *text, *dest;
int ch;
if(argc!=3)
{
printf("Use as: coder <text file<encoded file>");
exit(EXIT_FAILU RE);
}
if((text=fopen( argv[1],"r"))==NULL )
{
perror(argv[1]);
exit(EXIT_FAILU RE);
}
if((dest=fopen( argv[2],"r"))==NULL )
{
if(errno==ENOEN T)
{
if((dest=fopen( argv[2],"w"))==NULL )
{
perror(argv[2]);
exit(EXIT_FAILU RE);
}
}
else
{
perror(argv[2]);
exit(EXIT_FAILU RE);
}
}
else
{
fflush(stdin);
printf("The file to be written to exsits. Are you want to overwrite
it [y/n]");
ch=getchar();
do{
if((ch=='y')||( ch=='Y'))
{
fflush(stdin);
if((dest=fopen( argv[2],"w"))==NULL )
{
perror(argv[2]);
exit(EXIT_FAILU RE);
}
}
else if((ch=='n')||( ch=='N'))
{
fflush(stdin);
printf("Action Canceled");
exit(EXIT_FAILU RE);
}
else
{
printf("Invalid character, Try again\n");
fflush(stdin);
}
}while(ch=='y' || ch=='Y' || ch=='n' || ch=='N');
}
putc('h',dest);
fclose(text);
fclose(dest);
return 0;
}

Sorry for the 8-space tabs, but I copyed directly from MVC++ 6.0, and I
don't think notepad will do any good.

Aug 17 '06 #1
8 4488

Oops, sorry for the typo, it's "Too many files open", not "To many
files open".

Aug 17 '06 #2
someone wrote:
I'm making an program that encodes text, and then writes it into
another text file. The problem is that I can't seem to get the "file
exists" checking right. If I use the command (coder is the
application):
coder something.txt an_exsiting_fil e.txt, then if I chose to overwrite
"an_exsiting_fi le.txt", an error message pops up, saying that to many
files are open <EMFILE>. Any help would be appreciated. Here is the
code (incomplete but still compilable):
Your problem is with this construct:
ch=getchar();
do{
[...]
}while(ch=='y' || ch=='Y' || ch=='n' || ch=='N');
Observe that if you get a valid response (y/Y/n/N) the
loop repeats -- and since it doesn't read a new response, it
does the same thing it did before, and repeats, and does it
again, and repeats, and does it again ...

A few other points:

- fflush(stdin) has no meaning: You can only "flush" an
output stream. Try it on an input stream, and there's
no telling what might happen.

- On the other hand, you're missing an fflush(stdout)
call. If you want to be sure (or as sure as you can
be) that a prompt appears before you try to read the
response to it, fflush(stdout) after writing the prompt
and before reading the response.

- There's a laudable attempt to take different actions on
different error conditions. Unfortunately, C runs on a
wide variety of platforms whose file systems are not all
alike, and you cannot be sure that a given error condition
will produce a predictable Exxxxxx value. (In fact, you
cannot be sure that a failing fopen() will produce *any*
Exxxxxx value -- but most do.) Hence, all that part of
the logic is specific to particular platforms, and may not
work as intended on other machines.

--
Eric Sosman
es*****@acm-dot-org.invalid
Aug 17 '06 #3
Thanks for the help.

Aug 17 '06 #4
Thanks for the help, but now it says "Permission denied <EACCES>", any
idea on how to fix that?

Aug 17 '06 #5
someone wrote:
Thanks for the help, but now it says "Permission denied <EACCES>", any
idea on how to fix that?
Not in the context of platform-independent C, no.
The language doesn't define EACCES or what it means, so
you need to check your platform's documentation for hints
about what it's trying to tell you.

<off-topic>

It's *probably* trying to tell you that you lack the
appropriate permission for the kind of file access you're
attempting. Note that the permission failure might refer
to the target file itself, or to any of the directories
in its path -- and note that I've already said too much,
because C doesn't even have a notion of "directory. "

</off-topic>

--
Eric Sosman
es*****@acm-dot-org.invalid
Aug 17 '06 #6
someone wrote:
>
.... snip ...
>
Sorry for the 8-space tabs, but I copyed directly from MVC++ 6.0,
and I don't think notepad will do any good.
I am not willing to put up with them. All you had to do was
convert the tabs to a suitable number of spaces. If your editor
won't do it for you (most will) there are plenty of utilities
available for the job.

--
"The power of the Executive to cast a man into prison without
formulating any charge known to the law, and particularly to
deny him the judgement of his peers, is in the highest degree
odious and is the foundation of all totalitarian government
whether Nazi or Communist." -- W. Churchill, Nov 21, 1943

Aug 17 '06 #7
On 2006-08-17, someone <ra************ @gmail.comwrote :
I'm making an program that encodes text, and then writes it into
another text file. The problem is that I can't seem to get the "file
exists" checking right. If I use the command (coder is the
application):
coder something.txt an_exsiting_fil e.txt, then if I chose to overwrite
"an_exsiting_fi le.txt", an error message pops up, saying that to many
files are open <EMFILE>. Any help would be appreciated. Here is the
code (incomplete but still compilable):
Wow, did your space key bite you?
1) Don't use tabs on Usenet.
2) Space out your expressions. I've fixed both problems for my own
readability (and others').
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

int main (int argc, char *argv[])
{
FILE *text, *dest;
int ch;
if(argc != 3)
{
printf("Use as: coder <text file<encoded file>");
fputs (stderr, "Use as: coder input output");
exit(EXIT_FAILU RE);
}
if ((text = fopen (argv[1], "r")) == NULL)
{
perror (argv[1]);
Just so you know, this function isn't guaranteed to be portable. If that's
a concern, you should replace it with a potentially less descriptive fputs()
message.

....
>
Sorry for the 8-space tabs, but I copyed directly from MVC++ 6.0, and I
don't think notepad will do any good.
Sure it will. Type <tab>, copy it, open the "search and replace" window,
paste it into the "original text" box, and put how ever many spaces you
want (usually 2-4) into the "replacemen t text" box. (Note: I paraprased
heavily in my quoted text.)
You need to work on your error handling and coding style:

#define REQ_ARGS = 3;
#define PERROR(str) fputs (stderr, str)

int main (int argc, char *argv[])
{
int error = 0;
FILE *f1 = NULL;
FILE *f2 = NULL;

if (argc == 3)
{
f1 = fopen (argv[1], "r");
f2 = fopen (argv[2], "w");

if (f1 == NULL)
PERROR ("Couldn't open input file.");

if (f2 == NULL)
PERROR ("Couldn't open output file.");

error = !(f1 && f2);
} else {
PERROR ("Usage: coder input output");
error = 1;
}

if (!error)
{
/* Rest of code here. */
}

return error ? EXIT_FAILURE : 0;
}

See how I pulled that off using minimum indentation and only one exit point?
Many people consider more than 3 tabs bad code; this isn't always possible,
but it's a good rule of thumb.

--
Andrew Poelstra <http://www.wpsoftware. net/projects>
To reach me by email, use `apoelstra' at the above domain.
"Do BOTH ends of the cable need to be plugged in?" -Anon.
Aug 17 '06 #8
<OT>
On Thu, 17 Aug 2006 05:47:08 GMT, Andrew Poelstra
<ap*******@fals e.sitewrote:
Sorry for the 8-space tabs, but I copyed directly from MVC++ 6.0, and I
don't think notepad will do any good.

Sure it will. Type <tab>, copy it, open the "search and replace" window,
paste it into the "original text" box, and put how ever many spaces you
want (usually 2-4) into the "replacemen t text" box. (Note: I paraprased
heavily in my quoted text.)
Or in MSVS6 itself just select all (ctrl+A) and Edit / Advanced /
Untabify Selection. And/or set the editor options (for each necessary
filetype) to use spaces rather than tabs for indentation.

- David.Thompson1 at worldnet.att.ne t
Aug 28 '06 #9

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

Similar topics

1
1590
by: crispy | last post by:
I just upgraded to Office 2003 and none of my .mdb files open! Double-clicking an mdb file opens Access, but the database never opens. No error messages. No hourglass This happens to any mdb file I try to open. Does anyone know what could this be caused by? thanks. crispy
2
5450
by: Abhijit | last post by:
Hello everybody, I am getting a "segmentation fault" error when i compile the following piece of code. The program runs fine till it encounters the line of code saying" FILE_lic=fopen.........". And after that, it terminates saying segmentation fault. I had included the appropriate header files also! I couldn't makeout where the problem exists in the program. Can anyone solve this mistery!
4
1972
by: Maurice LING | last post by:
Hi, I'm trying to read some files (video files) that seems to have some errors in it. Basically, I cannot copy it out of discs as that gives me an error message but I can still play the file using a media player like VLC or QuickTime. I understand that copying a file will also invoke checking routines as well, and I am guessing that the file may have some parity-bit error or something. Am I able to use Python to force read the entire...
2
1637
by: google.groups.tr | last post by:
I have an Access 97 database that has a routine to generate a unique report as a PDF file and email it to one person, and loops about 300 times. Each instance through the loop uses a registry entry to pre-populate the desired filename for the PDF file it generates. The PDFs are generated, attached and emailed in the loop as they're intended. As it progresses, the focus tends to show the Acrobat window and the PDFs briefly appear as...
0
2108
by: Don | last post by:
I intermittently get a runtime Compilation Error that says 'The compiler failed with error code 2000'. It appears that a DLL cannot be found in the 'temporary asp.net files' directory. The Detailed Compiler Output is at the bottom of this post. This is a custom dll named UtilitiesINGR.dll that I put in the GAC. I added a machine.config entry to instruct my app to use the GAC entry. Now I need to remove the dll from the GAC so I can...
7
14621
by: Tyrone Showers | last post by:
I have a problem of getting the error "too many files open" and would like to trace my application. However, I have found nothing about how to display the current number of open files. Does anyone know what code is used to get the current number of open files? Also, is there a way to determine the number of open database connections?
8
3661
exoskeleton
by: exoskeleton | last post by:
hi dear experts.. i know you are aware of video once clicked it will open in media player...but how about gif files? im developing a site in php...when gif files were clicked, it will open in media player! please help me...i dont know what to do!
0
943
by: Jeff | last post by:
I'm still a bit new to vb.net using visual studio 2005, so bare with me if this question isn't well worded. On a rare occasion, my application will throw an error that I haven't yet tracked down but I'm getting close. It is occurring during an open Odbc connection to a mysql database. Afterward, the Odbc connection seems to be locked open if a user goes back in to the application and I'm not aware of how to close it properly.
1
1335
by: shashank1983 | last post by:
when i try to open an eclipse for java programming, i am getting error like an error has occured see the log files C:\Documents. can anybody help me t solve this problem...
0
8969
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8792
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9479
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9266
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9209
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8215
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6754
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6054
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4826
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.