How do I find the current path of my program? I need to access a directory called \data and ".\\data\\file" does do it.
How do I find the current path of my program? I need to access a directory called \data and ".\\data\\file" does do it.
Are you using MFC, or straight Win32?Originally posted by eam
How do I find the current path of my program? I need to access a directory called \data and ".\\data\\file" does do it.
If MFC:
1] Dialog or SDI/MDI?
GetCurrentDirectory() will tell you what the current directory is.
However, you shouldn't assume that the current directory is the directory where your application resides (not that you would).
If you want the directory that contains your running application, use GetModuleFileName().
gg
How'd I mess this up? No errors but it crashes my program... I tried I couple ways but this is the one that compiles:
Code:LPTSTR file; DWORD num = 100; GetModuleFileName(NULL,file,num);
You have to allocate memory for the path, eg:Code:TCHAR file[MAX_PATH]; GetModuleFileName(NULL,file,MAX_PATH);
CProgramming FAQ
Caution: this person may be a carrier of the misinformation virus.
There's one problem, the program name is added to the end of the path. I need to use that as a starting place for directories around the program. How should I remove the name out of it? Or is there a function that doesn't include the name?
You could use strrchr().
ggCode:char pathname[MAX_PATH]; GetModuleFileName(NULL, pathname, MAX_PATH); // chop off everything past last occurance of '\\' char *p = strrchr(pathname, '\\'); p[1] = NULL;
Okay, here's what I have:
Basicly what I want is to make a new var (file2) and add data\pic.bmp to the end. How should I fix that?Code:///works GetModuleFileName(NULL,file,MAX_PATH); char *p = strrchr(file, '\\'); p[1] = NULL; int t; for (t = 0; file[t] != '\0';t += 1) { file2[t] = file[t]; } //stops working below file2[t+=1] = 'd' ; file2[t+=1] = 'a' ; file2[t+=1] = 't' ; file2[t+=1] = 'a' ; file2[t+=1] = '\\' ; file2[t+=1] = 'p' ; file2[t+=1] = 'i' ; file2[t+=1] = 'c' ; file2[t+=1] = '.' ; file2[t+=1] = 'b' ; file2[t+=1] = 'm' ; file2[t+=1] = 'p' ; file2[t+=1] = '\0' ;
It's working perfectly now. Thanks to everyone who help.