Thread: file existance checking /w code

  1. #1
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765

    file existance checking /w code

    Code:
    int main()
    {
    repeat:
    FILE * Tmp;
    	clrscr();
    	printf.....
    ( irrelevant code before the case / switch statement )
    
    	case '1':
    	if((fopen("Pgm.exe", "r+") == 0))
    		{
    		printf("\n\nPgm.exe does not exist!\n");
    		printf("You need Pgm.exe\n\n");
    		printf("Press any key...");
    		getch();
    		}
    		else
    		{
    		fclose(Tmp);
    		if((fopen("2ndFile.ext", "r+") == 0))
    			{
    			printf("\n\n2ndFile.ext does not exist!\n");
    			printf("You need 2ndFile.ext\n\n");
    			printf("Press any key...");
    			getch();
    			}
    			else
    			{
    			fclose(Tmp);
    			system("PgmName");
    			}
    		}
    		goto repeat;
    		break;
    Access denied.

    Where am I messing up? I know I'm not closing the file somewhere. Possibly I read the file the wrong way? I'm using the open "command" to see if the file exists. If it does it will take action. I am also trying to use a way that does not write, save, or change the file in any way so it may be used if it is detected.

    I check for the existance of the program's engine, then if it exists I check for the existance of the secondary file which is loaded -through- the program's engine. If they both exist, it loads the secondary file through the program's engine by calling the program package's name.
    The world is waiting. I must leave you now.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > if((fopen("Pgm.exe", "r+") == 0))
    .exe files, especially ones which are executing can only be opened in read-only mode.

    try "r" only

  3. #3
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765

    :)

    Thank you, oh wise one, Salem.
    The world is waiting. I must leave you now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  2. Error when loading 3ds file
    By The Wazaa in forum C++ Programming
    Replies: 6
    Last Post: 01-24-2006, 08:27 AM
  3. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  4. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM