الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

How to get the current directory and the argument list?

مغلق
بدأه Genius في 24 نوفمبر 2001 · 3 رد · 389 مشاهدة · في ارشيف قسم C/C++
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

Dear all,

This is the first thread I sent to this board, please don't make me disappointed. :)

I have two easy questions:

1- I want to make a program that gives you the full path of itself, maybe this sounds silly, but I really want it.

For example if the program name is "program.exe", and it is in the "c:" folder; I want it to print in the screen "my path is c:program.exe" , isn't it easy? *

2- I want to make another program that gives you its Arguments. For example if your program is program.exe, and you run that program from the DOS by this command "program.exe -a -p -d", then it prints "My arguments are -a -p -d".

Please help me guys.

I tried several times to find some thing about that on the internet, but you are my last hope. :)

* note :

I know how to get the current directory by API, but what I want to know is how to find the full path of the file that is currently running.

#2

Do you need the program to be run on Windows as a consle or pure C, C++ ?

because in the first situation I could use an API to get the directory of the current file..

about the arguments, it's easy and can be done in the following way:

#include 

int main(int argc, char* argv[])
{
	argc--; argv++;

	if (argc > 0)
	{
		cout << "My arguments are : ";

		do {
			cout << *argv << " ";
			argc--;
			argv++;
		} while (argc > 0);

		cout << endl;
	}
	else
	{
		cout << "No arguments passed.." << endl;
	}

	return 0;
}

The arguments are passed to the program in the main's parameters. The first one is the number of the arguments including the name of the file. The scond one is array of pointer to char, each element points to a string.

Coding on the Cloud and for the Cloud!

My Blog

#3

The path, you can take it from argv[0] :

#include

#include

int main(int argc, char* argv[])

{

char strPath[200];

char *ptr;

strcpy(strPath, argv[0]);

ptr = strrchr(strPath, ''); // make sure it is ( ) without the space

if (ptr)

*ptr = '

Coding on the Cloud and for the Cloud!

My Blog

#4

Thank you so much, Hani. That was exactly what I wanted.

You answered the two questions by on answer

Thank you.

هذا الموضوع مغلق.

مواضيع مشابهة