C ++ takes the current path to the hacker !!!

Source: Internet
Author: User

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:
  1. # Include <iostream>
  2. Using namespace STD;
  3. Int main (INT argc, char ** argv)
  4. {
  5. Cout <argc <'/N ';
  6. Return 0;
  7. }
  8. 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:
  1. # Include <iostream>
  2. Using namespace STD;
  3. Int main (INT argc, char * argv [])
  4. {
  5. Cout <argv [0] <'/N ';
  6. Return 0;
  7. }
  8. 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:
  1. # Include <iostream>
  2. Using namespace STD;
  3. Int main (INT argc, char * argv [])
  4. {
  5. Cout <argv [0] <'/N ';
  6. Cout <argv [1] <'/N ';
  7. Cout <argv [2] <'/N ';
  8. Return 0;
  9. }
  10. 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:
  1. # Include <iostream>
  2. Using namespace STD;
  3. Int main (INT argc, char ** argv)
  4. {
  5. Char * P = & argv [0] [0];
  6. Int I;
  7. For (; * P! = 0; P ++)
  8. If (* P = '//')
  9. I = p-argv [0];
  10. Argv [0] [I] = 0;
  11. Cout <argv [0] <'/N ';
  12. Return 0;
  13. }
  14. 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.