# changing "active" dir

This is a discussion on changing "active" dir within the C Programming forums, part of the General Programming Boards category; Using gcc, @echo off cls REM Current directory is C:\ cd windows cd system ..what is the C equivilent of ...

1. ## 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

2. Not sure if it works for gcc but check the manual pages for chdir or _chdir.

3. ## 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;
system("choice /c:. /t:.,03 /n >NUL");
if ( ( Tmp = fopen("c:\\emulation\\mameb16\\mamepp.exe", "r+") ) == 0)
{
printf("Mamepp.exe was not found.  You need this file to run the game.\n\n");
printf("Press any key...");
getch();

hold_time=time(NULL);
Log = fopen("log.htm", "a");
fprintf(Log, "++++++++++++++++++++++++++++++\n");
fprintf(Log, ctime(&hold_time));
fprintf(Log, "1942\n");
fprintf(Log, "++++++++++++++++++++++++++++++\n\n");
}
else
{
fclose(Tmp);
if ( ( Tmp = fopen("c:\\emulation\\mameb16\\roms\\1942.zip", "r+") ) == 0)
{
printf("The rom was not found.  You need this file to run the game.\n\n");
printf("Press any key...");
getch();

hold_time=time(NULL);
fprintf(Log, "++++++++++++++++++++++++++++++\n");
fprintf(Log, ctime(&hold_time));
fprintf(Log, "1942\n");
fprintf(Log, "++++++++++++++++++++++++++++++\n\n");
}
else
{
fclose(Tmp);
hold_time=time(NULL);
fprintf(Log, "++++++++++++++++++++++++++++++\n");
fprintf(Log, ctime(&hold_time));
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("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, "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, "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.

4. ## 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

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

5. 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");
}