How do I get the double slash form of the path where the currently running. EXE is located? ---using GetModuleFileName

Source: Internet
Author: User

Let's take a look at a simple program:

#include <windows.h>
#include <iostream>
using namespace std;

int main ()
{
	char  szbuf[1025] = {0};   
	GetModuleFileName (NULL, szbuf, sizeof (SZBUF));
	cout << szbuf << Endl; C:\Documents and settings\administrator\ Desktop \cpp\test\debug\test.exe return

	0;
}

However, if you use the above road strength in the program, it is not, because \ in C language is the escape character, we look at the following:

#include <fstream>
using namespace std;

int main ()
{
	ofstream outfile ("C:\Documents and settings\administrator\ Desktop \mycpp\test.txt"); Test.txt files will not be generated
	outfile << "Hello World" << Endl;

	return 0;
}

And the following procedure is OK:

#include <fstream>
using namespace std;

int main ()
{
	ofstream outfile ("C:\\Documents and settings\\administrator\\ Desktop \\MYCPP\test.txt"); Will generate Test.txt file
	outfile << "Hello World" << Endl;

	return 0;
}


We continue to look at:

#include <windows.h>
#include <iostream>
using namespace std;

int main ()
{
	char  szbuf[1025] = {0};   
	GetModuleFileName (NULL, szbuf, sizeof (SZBUF));
	cout << szbuf << Endl;   C:\Documents and settings\administrator\ desktop \cpp\test\debug\test.exe

	if (0 = strcmp (szbuf, "c:\\ Documents and settings\\administrator\\ Desktop \\cpp\\test\\Debug\\test.exe ")"
	{
		cout << "yes" << Endl   Yes
	}
	else
	{
		cout << "no" << Endl;
	}

	return 0;
}

Don't be surprised, \ is the escape symbol, \ \ Only represents a \, so the following code is wrong:

int main ()
{
	char c = ' \ ';//Error return
	0;
}

Based on the above discussion, let's get back to the point where the code for the current path is:

#include <windows.h>
#include <iostream>
#include <fstream>
using namespace std;

int main ()
{
	char   szbuf[1025] = {0};   
	GetModuleFileName (NULL, szbuf, sizeof (SZBUF));
	
	Char *p = STRRCHR (szbuf, ' \ \ '); 
	*p = ' n '; 

	strcat (Szbuf, "\\test.txt");  To emphasize, strcat is very unsafe

	cout << szbuf << Endl;//It is double slash, output shows a single slash of Ofstream outfile

	(SZBUF);//Will generate TE St.txt file
    outfile << "Hello World" << Endl;

	return 0;
}

In short, understanding the escape symbol, everything is simple.

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.