I haven't written technical articles here for a long time. I can't help you. But at noon today, I think of good news. Today, I found a good thing, and now I am very happy. Haha, I can also be a hacker! Haha.
/--------------------------------------------------------------------------
First, we will introduce how to use C ++ to obtain the current path. In fact, the C language can also be used, but the output statement can be slightly changed.
Keywords: Main () function.
You are not familiar with the main () function. Some people know that the main () function can contain parameters. But have you used its parameters to obtain the current path? The results I checked on the network all use Win32 API functions, which is really troublesome. This is simple and easy to understand. Next I will talk about my thoughts:
The prototype of the main () function is "Main (INT argc, char ** argv)". The first parameter is the number of command line parameters. If the code is as follows:
Code:
- # Include <iostream>
- Using namespace STD;
- Int main (INT argc, char ** argv)
- {
- Cout <argc <'/N ';
- Return 0;
- }
- Code 1
The following result is displayed during direct running:
If you use the command line and add the parameter "Hello, how are you?", you will get the following result:
The second parameter of the main () function represents the pointer of the command line. The statement can also be: char * argv []. Let's look at the Code as follows:
Code:
- # Include <iostream>
- Using namespace STD;
- Int main (INT argc, char * argv [])
- {
- Cout <argv [0] <'/N ';
- Return 0;
- }
- Code 2
The following result is displayed during direct running:
When you use the command line to include parameters, you can write the code as follows:
Code:
- # Include <iostream>
- Using namespace STD;
- Int main (INT argc, char * argv [])
- {
- Cout <argv [0] <'/N ';
- Cout <argv [1] <'/N ';
- Cout <argv [2] <'/N ';
- Return 0;
- }
- Code 3
If you use the command line and add the parameter "Hello, how are you?", you will get the following result:
Note:
1. If example3 is run directly, an error occurs because argv [1] and argv [2] do not exist.
2. When using the command line, you must copy the program to the corresponding directory.
So far, do you have any ideas? That's right! You only need to remove the application name from the path indicated by the string argv [0. Hard? Very easy!
What do you know about pointers? Do you know what the two pointers mean by subtraction? In fact, we can think like this: traverse the string argv [0], find the last "/" character, and set it to 0, which is equivalent to truncating the string, in this way, only the current path is displayed!
Code:
- # Include <iostream>
- Using namespace STD;
- Int main (INT argc, char ** argv)
- {
- Char * P = & argv [0] [0];
- Int I;
- For (; * P! = 0; P ++)
- If (* P = '//')
- I = p-argv [0];
- Argv [0] [I] = 0;
- Cout <argv [0] <'/N ';
- Return 0;
- }
- Code 4
Of course, you also want to retain the character "/", that is, "argv [0] [I] = 0 in the tenth line of the above Code; "to" argv [0] [I + 1] = 0.
Now let's take a look at the application.
How is it? It's easy. In fact, this is only the first step. (Conclusion 1)