Shadow2531
[H]ard|Gawd
- Joined
- Jun 13, 2003
- Messages
- 1,670
Using Mingw 3.4.2 on WinXP Sp2
Say if I have:
"c:\directory\program.exe"
"c:\directory\file.txt"
and in program.exe, I have ifstream in("file.txt").
Then I:
start
run
cmd
cd \
"c:\directory\program.exe"
program.exe won't be able to find "file.txt", because it's looking for file.txt at "c:\file.txt" instead of "c:\directory\file.txt".
Basically, when I call a program from a different directory than the program is in, the current working directory is the directory I called the program from instead of the directory the app is in.
1.) Does that work the same way on other OSs like mac and linux?
2.) What's the best way for a program to figure what directory it is in no matter where it's called from that also works on linux if necessary?
For windows, I can do something like this:
However, WinXP supports longer paths than win9X/ME and maybe Win2k also. I want to be able to take advantage of longer paths in WinXP and other OSs that support longer paths. Do I have to use GetModuleFileNameExW to do that? What size should I use for the array then? 32768?
Also, should I use another method to get just the path (instead of copying to a string and then substringing it) ?
Do I have to worry about anything when using TCHAR instead of char?
That should cover most of my questions about windows, but what about linux? ( I'd be testing on debian woody with whatever gcc version that comes with it )
I've been told before that's there's no cross-OS way of doing it, so I'd have to set up #ifdef conditionals to do what I want.
Of course you all may have better ideas.
Thanks
Say if I have:
"c:\directory\program.exe"
"c:\directory\file.txt"
and in program.exe, I have ifstream in("file.txt").
Then I:
start
run
cmd
cd \
"c:\directory\program.exe"
program.exe won't be able to find "file.txt", because it's looking for file.txt at "c:\file.txt" instead of "c:\directory\file.txt".
Basically, when I call a program from a different directory than the program is in, the current working directory is the directory I called the program from instead of the directory the app is in.
1.) Does that work the same way on other OSs like mac and linux?
2.) What's the best way for a program to figure what directory it is in no matter where it's called from that also works on linux if necessary?
For windows, I can do something like this:
Code:
#include <iostream>
#include <string>
#include <fstream>
#include <windows.h>
using namespace std;
int main() {
TCHAR path[2048] = {0};
GetModuleFileName( NULL, path, 2048 );
const string exe_path( path );
const string file_path( exe_path.substr(0, exe_path.rfind("\\") + 1 ) + "file.txt" );
ifstream in( file_path.c_str() );
if (!in) {
return 1;
}
for (string s; getline(in,s); ) {
cout << s << endl;
}
}
However, WinXP supports longer paths than win9X/ME and maybe Win2k also. I want to be able to take advantage of longer paths in WinXP and other OSs that support longer paths. Do I have to use GetModuleFileNameExW to do that? What size should I use for the array then? 32768?
Also, should I use another method to get just the path (instead of copying to a string and then substringing it) ?
Do I have to worry about anything when using TCHAR instead of char?
That should cover most of my questions about windows, but what about linux? ( I'd be testing on debian woody with whatever gcc version that comes with it )
I've been told before that's there's no cross-OS way of doing it, so I'd have to set up #ifdef conditionals to do what I want.
Of course you all may have better ideas.
Thanks