S
someone
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_file.txt, then if I chose to overwrite
"an_exsiting_file.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_FAILURE);
}
if((text=fopen(argv[1],"r"))==NULL)
{
perror(argv[1]);
exit(EXIT_FAILURE);
}
if((dest=fopen(argv[2],"r"))==NULL)
{
if(errno==ENOENT)
{
if((dest=fopen(argv[2],"w"))==NULL)
{
perror(argv[2]);
exit(EXIT_FAILURE);
}
}
else
{
perror(argv[2]);
exit(EXIT_FAILURE);
}
}
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_FAILURE);
}
}
else if((ch=='n')||(ch=='N'))
{
fflush(stdin);
printf("Action Canceled");
exit(EXIT_FAILURE);
}
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.
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_file.txt, then if I chose to overwrite
"an_exsiting_file.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_FAILURE);
}
if((text=fopen(argv[1],"r"))==NULL)
{
perror(argv[1]);
exit(EXIT_FAILURE);
}
if((dest=fopen(argv[2],"r"))==NULL)
{
if(errno==ENOENT)
{
if((dest=fopen(argv[2],"w"))==NULL)
{
perror(argv[2]);
exit(EXIT_FAILURE);
}
}
else
{
perror(argv[2]);
exit(EXIT_FAILURE);
}
}
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_FAILURE);
}
}
else if((ch=='n')||(ch=='N'))
{
fflush(stdin);
printf("Action Canceled");
exit(EXIT_FAILURE);
}
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.