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

  1. #1
    Registered User
    Join Date
    Jan 2012
    Location
    Chennai, Tamil nadu, India
    Posts
    30

    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...???
    Please help me guys..

    Thanks in advance...!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.
    When you find it, you have your answer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Location
    Chennai, Tamil nadu, India
    Posts
    30
    Thanks for ur response...!
    Well, its pretty hard for me to understand...
    Quote Originally Posted by Salem View Post
    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"....???

    Quote Originally Posted by Salem View Post
    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.
    When you find it, you have your answer.
    It shows something like a jung (see that screenshot)..

    Could you please tell me what should I parse, what should I test..??
    How can I code that..? Please please please, tell me in a simple way..
    Attached Images Attached Images Need help in opening a file without specifying its path...-screenshot-5-jpg 

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 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++


    From your other threads, I thought you were more advanced than it seems you really are.

    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?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jan 2012
    Location
    Chennai, Tamil nadu, India
    Posts
    30
    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..??
    Last edited by Rehman khan; 10-20-2012 at 06:46 AM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Rehman khan View Post
    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++?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 11-30-2011, 12:49 AM
  2. a file path problem
    By fighter92 in forum C++ Programming
    Replies: 1
    Last Post: 06-21-2008, 05:50 AM
  3. Getting file name off path
    By cgod in forum C++ Programming
    Replies: 6
    Last Post: 09-22-2005, 10:45 AM
  4. Finding the path of a file
    By caduardo21 in forum C Programming
    Replies: 8
    Last Post: 08-09-2005, 11:00 AM
  5. can .exe find the path to a file?
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 06-12-2002, 01:08 PM