Need help in opening a file without specifying its path...

This is a discussion on Need help in opening a file without specifying its path... within the C++ Programming forums, part of the General Programming Boards category; Hello frnds... I know that if the executable (.exe) file and the required file lies on the same folder, then ...

1. Need help in opening a file without specifying its path...

Hello frnds... I know that if the executable (.exe) file and the required file lies on the same folder, then it can be processed without specifying its path.

I'm not talking about usual file operations..

In my program, before it ends, it should open and display an image..
For tat i used this command:
Code:
 system("start c:\\rehman\\image.jpg");
It actually works.. But, that executable file depends on the path of that image file...
Is there anyway for this to work such that the image file and the executable file lie on same folder..?
Is there any code for that?
So that i can move that bundle to various locations or other computer...

Am i clear...???

2. Step 1 is look at argv[0]
Code:
int main ( int argc, char *argv[] ) {
cout << argv[0] << endl;
}
This should be something like C:\foo\bar\prog.exe
In which case, you strip off the trailing pathname (giving C:\foo\bar\) and you append image.jpg

Step 2 is to look at the PATH environment variable
Code:
int main ( int argc, char *argv[] ) {
char *env = getenv("PATH");
if ( env ) {
cout << env << endl;
}
}
This is a series of paths like C:\foo;C:\bar
Parse this to extract each path in turn, then test to see if C:\foo\image.jpg and C:\foo\prog.exe exist.

3. Thanks for ur response...!
Well, its pretty hard for me to understand...
Originally Posted by Salem
This should be something like C:\foo\bar\prog.exe
In which case, you strip off the trailing pathname (giving C:\foo\bar\) and you append image.jpg
I should move that image.jpg to the folder where the .exe file exist.

Is that what you meant my "append image.jpg"....???

Originally Posted by Salem
This is a series of paths like C:\foo;C:\bar
Parse this to extract each path in turn, then test to see if C:\foo\image.jpg and C:\foo\prog.exe exist.
It shows something like a jung (see that screenshot)..

Could you please tell me what should I parse, what should I test..??

4. > I should move that image.jpg to the folder where the .exe file exist.
I thought that is what you wanted in post #1
"Is there anyway for this to work such that the image file and the executable file lie on same folder..?"

Which of these do you prefer?
#include <string.h> // C
#include <string> // C++

If you can't manage these steps, then you need to practice the basics some more.
s1 = C:\foo\bar\prog.exe
s2 = C:\foo\bar\
s3 = C:\foo\bar\image.jpg

So from one of the following
char s1[] = "C:\foo\bar\prog.exe"; // C
string s1 = "C:\foo\bar\prog.exe"; // C++

Can you write some code (in C or C++) which eventually gives you C:\foo\bar\image.jpg in a variable?

5. Its okay..!! I can write code that gives the path and store that in a variable... say s3.

Then, what's next...??

I prefer C...
why isn't that possible with c..??

6. So create another string (s4) which looks like
Code:
char s4[100];
strcpy(s4,"start ");
strcat(s4,s3);
system(s4);
> why isn't that possible with c..??
Because you don't know enough of it yet.

7. Originally Posted by Rehman khan
Its okay..!! I can write code that gives the path and store that in a variable... say s3.

Then, what's next...??

I prefer C...
why isn't that possible with c..??
Then why are you posting in C++?