Thread: changing "active" dir

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

    changing "active" dir

    Using gcc,

    @echo off
    cls
    REM Current directory is C:\
    cd windows
    cd system

    ..what is the C equivilent of this?

    system("cd\\windows\\system"); doesn't seem to work for me
    The world is waiting. I must leave you now.

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Not sure if it works for gcc but check the manual pages for chdir or _chdir.

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

    I *think* it does

    Hello Monster,

    I'm pretty sure chdir works fine for me, using gcc. I tested with a simple program I wrote that opened a document or executed a program that wasn't in the working directory ( without providing the path to the file ). This worked. It opened just as if I were to add "path c:\path\to\my\stuff" into my program.

    I have a program that I am trying to break down, because it compiles fine, yet when excecuting certain parts ot it, it bombs back to Windows saying, "This program has performed an illegal operation and will be shut down." - Hum, why?

    Code:
    int run_1942()
    {
    FILE * Tmp;
    FILE * Log;
    time_t hold_time;
    	printf("Loading 1942...");
    	system("choice /c:. /t:.,03 /n >NUL");
    	if ( ( Tmp = fopen("c:\\emulation\\mameb16\\mamepp.exe", "r+") ) == 0)
    	{
    	// Emulator was not found.  Display a message.
    	printf("\n\nThere was an error loading 1942!\n");
    	printf("Mamepp.exe was not found.  You need this file to run the game.\n\n");
    	printf("Press any key...");
    	getch();
    	
    	// Emulator was not found.  Log the error.	
    	hold_time=time(NULL);
        Log = fopen("log.htm", "a");
    	fprintf(Log, "++++++++++++++++++++++++++++++\n");
    	fprintf(Log, ctime(&hold_time));
    	fprintf(Log, "Menu errored:\n");
    	fprintf(Log, "1942\n");
    	fprintf(Log, "Emulator was not found.\n");
    	fprintf(Log, "++++++++++++++++++++++++++++++\n\n");
    	}
    	else
    	{
    		fclose(Tmp);
    		if ( ( Tmp = fopen("c:\\emulation\\mameb16\\roms\\1942.zip", "r+") ) == 0)
    		{
    			// Rom was not found.  Display a message.
    			printf("\n\nThere was an error loading 1942!\n");
    			printf("The rom was not found.  You need this file to run the game.\n\n");
    			printf("Press any key...");
    			getch();
    					
    			// Rom was not found.  Log the error.
    			hold_time=time(NULL);
    			fprintf(Log, "++++++++++++++++++++++++++++++\n");
    			fprintf(Log, ctime(&hold_time));
    			fprintf(Log, "Menu errored:\n");
    			fprintf(Log, "1942\n");
    			fprintf(Log, "Rom file was not found.\n");
    			fprintf(Log, "++++++++++++++++++++++++++++++\n\n");
    		}
    		else
    		{
    			fclose(Tmp);
    			hold_time=time(NULL);
    			fprintf(Log, "++++++++++++++++++++++++++++++\n");
    			fprintf(Log, ctime(&hold_time));
    			fprintf(Log, "Menu ran:\n");
    			fprintf(Log, "1942\n");
    			fprintf(Log, "++++++++++++++++++++++++++++++\n\n");
                    chdir("mameb16");  // The path were mamepp.exe exists
    			if(system("mamepp 1942 >NUL") == 1)
    			{
    				// Rom failed to load.  Display a message.
    				printf("\n\nThere was an error loading 1942!\n");
    				printf("Possible causes:\n");
    				printf("The romset is incomplete.\n");
    				printf("The romset is corrupt.\n");
    				printf("There is not enough memory to run this game.\n\n");
    				printf("Press any key...");
    				getch();
    							
    				// Rom failed to load.  Log the error.
    				hold_time=time(NULL);
    				fprintf(Log, "++++++++++++++++++++++++++++++\n");
    				fprintf(Log, ctime(&hold_time));
    				fprintf(Log, "Menu errored:\n");
    				fprintf(Log, "1942\n");
    				fprintf(Log, "Rom file failed to load.\n");
    				fprintf(Log, "Run a check on the romset.\n");
    				fprintf(Log, "++++++++++++++++++++++++++++++\n\n");
    			}
    			else
    			{
    				// All files were found.  Files loaded fine.
    				hold_time=time(NULL);
    				fprintf(Log, "++++++++++++++++++++++++++++++\n");
    				fprintf(Log, ctime(&hold_time));
    				fprintf(Log, "Menu closed:\n");
    				fprintf(Log, "1942\n");
    				fprintf(Log, "++++++++++++++++++++++++++++++\n\n");
    			}
    		}
    	}
    	system("cd..");
    return 0;
    }
    Keep in mind, this code is from part of a large program that needs to be heavily modularized.

    I was using Turbo C++ before to write this program. I tried to compile it with Turbo C++ upon completion ( 7 1/2 k in lines ), and it runs out of memory during compilation. I run out and get GCC. I got the Win32 version, btw. After some configuring ( adding code into a newly created standard include file for clearing the screen, and forcing full screen upon startup ), I get it to compile. ( I am taking it one step at a time ). Ok, it compiles! I then convert my other 2 WIP programs ( much much smaller, few hundred lines ... ) over to compile with gcc. They compile, and execute just fine.

    Everything compiles, my special function prototypes work just fine, no errors are given on compilation. This large program starts up fine, and idles just fine, but whenever you go to execute one of the function prototypes, it crashes.

    This, is why I asked the chdir question. I am trying to break the program down. There are several other functions identical to this one, only they execute other "games". They all cause an error upon execution. I don't know if it is the ....I don't know what it is.
    The world is waiting. I must leave you now.

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

    I am breaking it down more as I go

    ...or atleast trying to lol.

    My directory structure for where this program will be executing at ( along with the other programs and zips it will be calling ) is as follows

    c:\emulation
    This is where this proggram will be executed from.

    Subdir's of c:\emulation
    c:\emulation\mameb16
    c:\emulation\mameb4
    c:\emulation\mame54

    In the base of each of those mame subdir's there is a mame executable.

    mameb16 = mamepp.exe
    mameb4 = mameppro.exe
    mame54 = arcade.exe

    There is a subdirectory inside each mame folder titled roms. Regardles of which mame folder it is, the subirectory is always titled roms. I created another small troubleshooting program and placed it in the c:\emulation folder:

    Code:
    int main()
    {
    chdir("mameb16");
    			if(system("mamepp 1942 >NUL") == 1)
    			{
    			    printf("No, No");
    			}
    			else
    			{
                                printf("Yes, woohoo");
    			}
    }
    This works just fine. It loads the game, then when it cames back to the prompt it prints, "Yes, woohoo".:P

    In the previous post, I dont know if it is the file exstance checking on the executable involved, along with the zip involved, or what is is. Somewhere along the line, it is crashing
    The world is waiting. I must leave you now.

  5. #5
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    If you want to check if a file exists you can use the _access function.

    Code:
    #include <io.h>
    
    if(_access(Tmp, 0) == -1)
    {
      printf("File does not exists\n");
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 04-25-2007, 02:48 PM
  2. Changing windows without changing?
    By Lionmane in forum Windows Programming
    Replies: 7
    Last Post: 10-19-2005, 11:41 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Changing the active dir
    By pinkcheese in forum Windows Programming
    Replies: 5
    Last Post: 05-24-2002, 01:55 AM