CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Mar 2008
    Posts
    7

    [RESOLVED] opening files with changing file paths

    When I'm programing I move between the school computers and home and I have the program files on a data key. My computer and the school computers assign different drive letters to data keys. Is there a way to store the directory the program in executing form to a string and thus know what file path to use? It would also be helpful incase you moved the program around.
    while ( ! ( succeed = try() ) );

  2. #2
    Join Date
    Jun 2008
    Location
    Oregon, USA
    Posts
    63

    Re: opening files with changing file paths

    If you and the school both use Windows XP (I don't know how to use batch on other versions of windows), I use batch scripting very frequently in my computer (i.e. a quick, easy to use compiler for my .cpp files), so that may be something to look into.
    (a quick reference is to open up command prompt and type "help [command]" or "[command] /?" or "help" for a list of commands)

    I know it has nothing really to do with C++, but it isn't a bad Windows XP solution.

    example set:

    prog.cpp
    Code:
    #include <iostream>
    using namespace std;
    
    int main(int argc, char** argv) {
    
    if (argc == 2) {
    cout << "Highest drive found is: " << argv[1] << endl;
    }
    
    return 0;
    }
    (note: first find out what the highest probable drive letter is, like F:, which normally occurs when only 2 devices are connected (A, C, and D are floppy drive, HDD, CD drive)

    NOTE: The below script is only useful if your data key is the latest device to be connected to windows and therefore receives the highest letter(closest to Z), as would be expected

    script.bat (REM = remark, or comment line)
    Code:
    if exist G: goto g
    REM goes to label 'g' if drive G: is found
    if exist F: goto f
    if exist E: goto e
    
    :g
    prog G:
    REM launches prog.exe with G: as first argument, when prog.exe is in the same directory
    exit /b
    
    :f
    prog F:
    exit /b
    
    :e
    prog E:
    exit /b
    lastly, right-click a .bat file and click edit to change it. If you don't know how to make batch scripts, I can make one for you if you tell me:
    - folders on the data key
    - what you are doing with the files (reading, copying to main disk, etc.)
    - a file or folder that is directly inside your data key (like WINDOWS is in C:\)
    - all drive letters used on yours and the school's computer
    Last edited by Bluefox815; June 24th, 2008 at 05:57 PM.

  3. #3
    Join Date
    Mar 2008
    Posts
    7

    Re: opening files with changing file paths

    I actually use Ubuntu/ Windows ME at home and 2000 at school.
    I don't feel that I'm competent enough to be integrating scripts into my compiler.

    I guess there isn't an input stream that you'd be able to pull the file path off of. I think I'll go ask Google again.

    looking at your examples, I'm wondering what the parameters you put in the function main() are for. The book I'm using to learn hasn't gone over that yet. I think I'll go ask Google about that to.
    Last edited by Gulyman; June 24th, 2008 at 07:35 PM.
    while ( ! ( succeed = try() ) );

  4. #4
    Join Date
    Jun 2008
    Location
    Oregon, USA
    Posts
    63

    Re: opening files with changing file paths

    The parameters in main I used are for command line arguments, which are separated by spaces following the program name on the command line.

    if I typed "prog hello world" on the command line in the same directory as the below program (this is how it works in Windows).

    Code:
    // prog.exe
    
    #include <iostream>
    using namespace std;
    
    // notice that argv is char** (char* argv[] also works, but is confusing)
    int main(int argc, char** argv) {
    // argc tells you how many arguments there are (always true (argc >= 1) it includes the program name)
    // argv[0] is always equal to the program name (idk why, could be useful if a user renamed the executable though)
    for (int i = 1; i < argc; i++) {
    cout << "Argument " << i << ": " << argv[i] << endl;
    }
    return 0;
    }
    the output would be:

    Argument 1: hello
    Argument 2: world

    and for "prog hello world" argc would equal 3
    Last edited by Bluefox815; June 25th, 2008 at 12:49 PM.

  5. #5
    Join Date
    Mar 2008
    Posts
    7

    Re: opening files with changing file paths

    That's cool. I wonder if you typed in
    "pwd | prog"

    pwd-print working directory(linux)

    if it would take the directory you're working in and use it for argv?

    Anyways I'm going to put this thread as resolved since no one else seems to be answering.

    Thanks for talking.
    while ( ! ( succeed = try() ) );

  6. #6
    Join Date
    Jun 2008
    Location
    Oregon, USA
    Posts
    63

    Re: [RESOLVED] opening files with changing file paths

    if you were to type "pwd.exe | prog" (pwd and pwd.exe are no different on Windows command line)

    you would get this result:

    argc = 3;

    argv[0] = "pwd"
    argv[1] = "|";
    argv[2] = "prog";

  7. #7
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: [RESOLVED] opening files with changing file paths

    If you have not found it out already...

    argv[0] contains the path and filename of your executable. Can't test and don't remember if this holds for both ME and 2000 but you can easily test it by excuting the following program
    Code:
    #include <stdio.h>
    int main(int argc, char** argv)
    {
      printf( "%s\n", argv[0] );
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured