当前位置:编程学习 > C/C++ >>

C++ 命令行

我想让我编的程序获取该程序的文件所在路径,控制台程序即是argv[0],MFC 程序调用AfxGetApp()即可获得,

但程序一旦添加了<命令行>,路径就默认设成了"C:\Documen..\Administrator\",导致用简略文件名时出现一些错误,

这时候应该怎么获取该程序的文件所在路径呢?

 

追问:给个例子好吗?
答案:
The GetFullPathName function retrieves the full path and file name of a specified file.


DWORD GetFullPathName(
LPCTSTR lpFileName,
DWORD nBufferLength,
LPTSTR lpBuffer,
LPTSTR* lpFilePart
);

Parameters
lpFileName
[in] A pointer to a null-terminated string that specifies a valid file name.
This string can use short (the 8.3 form) or long file names.

nBufferLength
[in] The size of the buffer to receive the null-terminated string for the drive and path, in TCHARs.
lpBuffer
[out] A pointer to a buffer that receives the null-terminated string for the drive and path.
lpFilePart
[out] A pointer to a buffer that receives the address (in lpBuffer) of the final file name component in the path.
If lpBuffer points to a directory and not a file, lpFilePart receives 0 (zero).

Return Values
If the function succeeds, the return value is the length of the string copied to lpBuffer, not including the terminating null character, in TCHARs.

If the lpBuffer buffer is too small to contain the path, the return value is the size of the buffer that is required to hold the path and the terminating null character, in TCHARs. Therefore, if the return value is greater than nBufferLength, call the function again with a buffer that is large enough to hold the path.

If the function fails for any other reason, the return value is 0 (zero). To get extended error information, call GetLastError.

上一个:怎样学好C++
下一个:高分悬赏C++实验

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,