Click to See Complete Forum and Search --> : [RESOLVED] opening files with changing file paths


Gulyman
June 24th, 2008, 01:52 PM
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.

Bluefox815
June 24th, 2008, 05:40 PM
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

#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)

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

Gulyman
June 24th, 2008, 07:31 PM
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.

Bluefox815
June 25th, 2008, 12:46 PM
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).


// 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

Gulyman
June 25th, 2008, 08:17 PM
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.

Bluefox815
July 1st, 2008, 06:09 PM
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";

S_M_A
July 2nd, 2008, 04:50 PM
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#include <stdio.h>
int main(int argc, char** argv)
{
printf( "%s\n", argv[0] );
}