C++如何获取当前程序目录

suifengjz 2017-04-24 12:20:13
VB中有个APP.Path, C++该怎么写代码,才能获得当前程序的路径
...全文
4448 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
njlc 2017-04-26
  • 打赏
  • 举报
回复
问题怎么解决的,楼主?
starytx 2017-04-25
  • 打赏
  • 举报
回复
MFC的话可以放在CString对象里,然后有replace方法,替换就行了 stl里有std::string
suifengjz 2017-04-25
  • 打赏
  • 举报
回复
引用 7 楼 starytx 的回复:
TCHAR szDir[MAX_PATH] = { 0 }; GetCurrentDirectory(MAX_PATH, szDir); 这就是exe所在的目录
或许是我表达能力不清楚,我是说我想要个双斜杠的路径,,怎么把获得的单斜杠路径转换为双斜杠的 ,因为程序下面还有个自己目录需要用到,我想通过获得路径的方法添加子级目录,
赵4老师 2017-04-25
  • 打赏
  • 举报
回复
_getcwd, _wgetcwd Get the current working directory. char *_getcwd( char *buffer, int maxlen ); wchar_t *_wgetcwd( wchar_t *buffer, int maxlen ); Routine Required Header Compatibility _getcwd <direct.h> Win 95, Win NT _wgetcwd <direct.h> or <wchar.h> Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value Each of these functions returns a pointer to buffer. A NULL return value indicates an error, and errno is set either to ENOMEM, indicating that there is insufficient memory to allocate maxlen bytes (when a NULL argument is given as buffer), or to ERANGE, indicating that the path is longer than maxlen characters. Parameters buffer Storage location for path maxlen Maximum length of path in characters: char for _getcwd and wchar_t for _wgetcwd Remarks The _getcwd function gets the full path of the current working directory for the default drive and stores it at buffer. The integer argument maxlen specifies the maximum length for the path. An error occurs if the length of the path (including the terminating null character) exceeds maxlen. The buffer argument can be NULL; a buffer of at least size maxlen (more only if necessary) will automatically be allocated, using malloc, to store the path. This buffer can later be freed by calling free and passing it the _getcwd return value (a pointer to the allocated buffer). _getcwd returns a string that represents the path of the current working directory. If the current working directory is the root, the string ends with a backslash ( \ ). If the current working directory is a directory other than the root, the string ends with the directory name and not with a backslash. _wgetcwd is a wide-character version of _getcwd; the buffer argument and return value of _wgetcwd are wide-character strings. _wgetcwd and _getcwd behave identically otherwise. Generic-Text Routine Mappings TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined _tgetcwd _getcwd _getcwd _wgetcwd Example // GETCWD.C /* This program places the name of the current directory in the * buffer array, then displays the name of the current directory * on the screen. Specifying a length of _MAX_PATH leaves room * for the longest legal path name. */ #include <direct.h> #include <stdlib.h> #include <stdio.h> void main( void ) { char buffer[_MAX_PATH]; /* Get the current working directory: */ if( _getcwd( buffer, _MAX_PATH ) == NULL ) perror( "_getcwd error" ); else printf( "%s\n", buffer ); } Output C:\code Directory Control Routines See Also _chdir, _mkdir, _rmdir
suifengjz 2017-04-25
  • 打赏
  • 举报
回复
引用 23 楼 zhao4zhong1 的回复:
看21楼
赵老师,是不是我系统或电脑的问题啊,
引用 23 楼 zhao4zhong1 的回复:
看21楼
感谢赵老师的热心帮助,问题以解决。
suifengjz 2017-04-25
  • 打赏
  • 举报
回复
引用 23 楼 zhao4zhong1 的回复:
看21楼
程序结束的时候会报错,是不是我电脑的问题,
suifengjz 2017-04-25
  • 打赏
  • 举报
回复
引用 23 楼 zhao4zhong1 的回复:
看21楼
完全用的21楼代码,
赵4老师 2017-04-25
  • 打赏
  • 举报
回复
看21楼
suifengjz 2017-04-25
  • 打赏
  • 举报
回复
引用 20 楼 zhao4zhong1 的回复:
[quote=引用 19 楼 suifengjz 的回复:] [quote=引用 18 楼 zhao4zhong1 的回复:] _tprintf(_T("The current directory is:[%s]\n"),buffer);
那个警告不去除不行,会发生缓冲区溢出[/quote] 我不是用红色标记强调了我少写了一个)吗? 你加上就应该不会报错了。 或者你干脆注释掉这行 //_tprintf(_T("The current directory is:[%s]\n"),buffer);[/quote]我注释掉了,关键是这行代码问题 _tcscat_s(buffer,sizeof(buffer),s);,不注释掉这行会报错
赵4老师 2017-04-25
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <direct.h>
#include <tchar.h>
#include <windows.h>
int main() {
    TCHAR buffer[MAX_PATH];
    _tgetcwd(buffer, MAX_PATH);
    _tprintf(_T("The current directory is:[%s]\n"),buffer);
    TCHAR *s=_T("\\PIC\\1.bmp");
    _tcscat_s(buffer,sizeof(buffer),s);
    LPCTSTR lpszName =LPCTSTR(buffer);
    _tprintf(_T("lpszName:[%s]\n"),lpszName);
    return 0;
}
//The current directory is:[C:\test\Debug]
//lpszName:[C:\test\Debug\PIC\1.bmp]
//
赵4老师 2017-04-25
  • 打赏
  • 举报
回复
引用 19 楼 suifengjz 的回复:
[quote=引用 18 楼 zhao4zhong1 的回复:] _tprintf(_T("The current directory is:[%s]\n"),buffer);
那个警告不去除不行,会发生缓冲区溢出[/quote] 我不是用红色标记强调了我少写了一个)吗? 你加上就应该不会报错了。 或者你干脆注释掉这行 //_tprintf(_T("The current directory is:[%s]\n"),buffer);
suifengjz 2017-04-25
  • 打赏
  • 举报
回复
引用 18 楼 zhao4zhong1 的回复:
_tprintf(_T("The current directory is:[%s]\n"),buffer);
那个警告不去除不行,会发生缓冲区溢出
赵4老师 2017-04-25
  • 打赏
  • 举报
回复
_tprintf(_T("The current directory is:[%s]\n"),buffer);
suifengjz 2017-04-25
  • 打赏
  • 举报
回复
引用 13 楼 zhao4zhong1 的回复:
[quote=引用 10 楼 suifengjz 的回复:] [quote=引用 7 楼 starytx 的回复:] TCHAR szDir[MAX_PATH] = { 0 }; GetCurrentDirectory(MAX_PATH, szDir); 这就是exe所在的目录
或许是我表达能力不清楚,我是说我想要个双斜杠的路径,,怎么把获得的单斜杠路径转换为双斜杠的 ,因为程序下面还有个自己目录需要用到,我想通过获得路径的方法添加子级目录,[/quote] 双斜杠只存在编译前的源代码中,变量的实际值中是单斜杠。[/quote]
引用 16 楼 zhao4zhong1 的回复:
    TCHAR buffer[MAX_PATH];
    _tgetcwd(buffer, MAX_PATH);
    _tprintf(_T("The current directory is:[%s]\n",buffer);
      TCHAR *s=_T("\\PIC\\1.bmp");
    _tcscat_s(buffer,sizeof(buffer),s);
    LPCTSTR lpszName =LPCTSTR(buffer);
试了下代码可以,显示一个警告 _tprintf(_T("The current directory is:[%s]\n",buffer);_T宏的实参太多
赵4老师 2017-04-25
  • 打赏
  • 举报
回复
    TCHAR buffer[MAX_PATH];
    _tgetcwd(buffer, MAX_PATH);
    _tprintf(_T("The current directory is:[%s]\n",buffer);
      TCHAR *s=_T("\\PIC\\1.bmp");
    _tcscat_s(buffer,sizeof(buffer),s);
    LPCTSTR lpszName =LPCTSTR(buffer);
suifengjz 2017-04-25
  • 打赏
  • 举报
回复
引用 14 楼 suifengjz 的回复:
[quote=引用 11 楼 starytx 的回复:] MFC的话可以放在CString对象里,然后有replace方法,替换就行了 stl里有std::string

//#include "stdafx.h"
 #include"atlstr.h"
 #include <tchar.h>
#include "FindProc.h"  
#include <iostream> 
#include  <direct.h>  
#include  <stdio.h>  
#include <windows.h> 
#include <string> 
#include <iterator> 
#include <algorithm> 
using namespace std;  
#pragma comment( lib, "..\\debug\\FindProc.lib" ) 

extern int FindBmp(int x1,int y1,int x2,int y2,LPCTSTR lpszName,int &fx,int &fy);
extern int FindBmpEx(int x1,int y1,int x2,int y2,LPCTSTR lpszName,int backcolor,int errorcolor,int errorcount,int &fx,int &fy);


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

{  
	char buffer[MAX_PATH];   
	_getcwd(buffer, MAX_PATH);   
	printf( "The   current   directory   is:   %s\n ",   buffer);
	  char *s="\\PIC\\1.bmp";
	strcat_s(buffer,sizeof(buffer),s);

string strinfo=buffer;
	cout<<"old string is :"<<endl<<strinfo<<endl; 
replace(strinfo.begin(),strinfo.end(),'\\','/'); 
cout<<"new string is :"<<endl<<strinfo<<endl; 

	LPCTSTR lpszName =LPCTSTR(buffer);

		int fx,fy;
 		cout<<FindBmp(0,0,1920,1080,lpszName,  fx, fy)<<"\n";
		
		//忽略位图backcolor(背景色)颜色点,颜色偏差errorcolor,允许不匹配点的个数errorcount
		//cout<<FindBmpEx(0,0,1920,1080, lpszName,0xF4F4F4,0x100000,10,fx,fy)<<"\n";
		cout<<"坐标X:"<<fx <<"\n";
		cout<<"坐标Y:"<<fy<<"\n";
		system("pause");    
		return 0;  
	}  
是这个样子吗,但是转换后有乱码,不知道该怎么修改[/quote]就是调试到这句的时候 LPCTSTR lpszName =LPCTSTR(buffer);显示乱码
suifengjz 2017-04-25
  • 打赏
  • 举报
回复
引用 11 楼 starytx 的回复:
MFC的话可以放在CString对象里,然后有replace方法,替换就行了 stl里有std::string

//#include "stdafx.h"
 #include"atlstr.h"
 #include <tchar.h>
#include "FindProc.h"  
#include <iostream> 
#include  <direct.h>  
#include  <stdio.h>  
#include <windows.h> 
#include <string> 
#include <iterator> 
#include <algorithm> 
using namespace std;  
#pragma comment( lib, "..\\debug\\FindProc.lib" ) 

extern int FindBmp(int x1,int y1,int x2,int y2,LPCTSTR lpszName,int &fx,int &fy);
extern int FindBmpEx(int x1,int y1,int x2,int y2,LPCTSTR lpszName,int backcolor,int errorcolor,int errorcount,int &fx,int &fy);


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

{  
	char buffer[MAX_PATH];   
	_getcwd(buffer, MAX_PATH);   
	printf( "The   current   directory   is:   %s\n ",   buffer);
	  char *s="\\PIC\\1.bmp";
	strcat_s(buffer,sizeof(buffer),s);

string strinfo=buffer;
	cout<<"old string is :"<<endl<<strinfo<<endl; 
replace(strinfo.begin(),strinfo.end(),'\\','/'); 
cout<<"new string is :"<<endl<<strinfo<<endl; 

	LPCTSTR lpszName =LPCTSTR(buffer);

		int fx,fy;
 		cout<<FindBmp(0,0,1920,1080,lpszName,  fx, fy)<<"\n";
		
		//忽略位图backcolor(背景色)颜色点,颜色偏差errorcolor,允许不匹配点的个数errorcount
		//cout<<FindBmpEx(0,0,1920,1080, lpszName,0xF4F4F4,0x100000,10,fx,fy)<<"\n";
		cout<<"坐标X:"<<fx <<"\n";
		cout<<"坐标Y:"<<fy<<"\n";
		system("pause");    
		return 0;  
	}  
是这个样子吗,但是转换后有乱码,不知道该怎么修改
赵4老师 2017-04-25
  • 打赏
  • 举报
回复
引用 10 楼 suifengjz 的回复:
[quote=引用 7 楼 starytx 的回复:] TCHAR szDir[MAX_PATH] = { 0 }; GetCurrentDirectory(MAX_PATH, szDir); 这就是exe所在的目录
或许是我表达能力不清楚,我是说我想要个双斜杠的路径,,怎么把获得的单斜杠路径转换为双斜杠的 ,因为程序下面还有个自己目录需要用到,我想通过获得路径的方法添加子级目录,[/quote] 双斜杠只存在编译前的源代码中,变量的实际值中是单斜杠。
赵4老师 2017-04-25
  • 打赏
  • 举报
回复
GETCWD Section: Linux Programmer's Manual (3) Updated: 2002-04-22 -------------------------------------------------------------------------------- NAME getcwd, get_current_dir_name, getwd - Get current working directory SYNOPSIS #include <unistd.h> char *getcwd(char *buf, size_t size); char *get_current_dir_name(void); char *getwd(char *buf); DESCRIPTION The getcwd() function copies an absolute pathname of the current working directory to the array pointed to by buf, which is of length size. If the current absolute path name would require a buffer longer than size elements, NULL is returned, and errno is set to ERANGE; an application should check for this error, and allocate a larger buffer if necessary. If buf is NULL, the behaviour of getcwd() is undefined. As an extension to the POSIX.1 standard, Linux (libc4, libc5, glibc) getcwd() allocates the buffer dynamically using malloc() if buf is NULL on call. In this case, the allocated buffer has the length size unless size is zero, when buf is allocated as big as necessary. It is possible (and, indeed, advisable) to free() the buffers if they have been obtained this way. get_current_dir_name, which is only prototyped if _GNU_SOURCE is defined, will malloc(3) an array big enough to hold the current directory name. If the environment variable PWD is set, and its value is correct, then that value will be returned. getwd, which is only prototyped if _BSD_SOURCE or _XOPEN_SOURCE_EXTENDED is defined, will not malloc(3) any memory. The buf argument should be a pointer to an array at least PATH_MAX bytes long. getwd does only return the first PATH_MAX bytes of the actual pathname. Note that PATH_MAX need not be a compile-time constant; it may depend on the filesystem and may even be unlimited. For portability and security reasons, use of getwd is deprecated. RETURN VALUE NULL on failure with errno set accordingly, and buf on success. The contents of the array pointed to by buf is undefined on error. ERRORS EACCES Permission to read or search a component of the file name was denied. EFAULT buf points to a bad address. EINVAL The size argument is zero and buf is not a null pointer. ENOENT The current working directory has been unlinked. ERANGE The size argument is less than the length of the working directory name. You need to allocate a bigger array and try again. NOTES Under Linux, the function getcwd() is a system call (since 2.1.92). On older systems it would query /proc/self/cwd. If both system call and proc file system are missing, a generic implementation is called. Only in that case can these calls fail under Linux with EACCES. These functions are often used to save the location of the current working directory for the purpose of returning to it later. Opening the current directory (".") and calling fchdir(2) to return is usually a faster and more reliable alternative when sufficiently many file descriptors are available, especially on platforms other than Linux. CONFORMING TO POSIX.1 SEE ALSO chdir(2), fchdir(2), open(2), unlink(2), free(3), malloc(3) --------------------------------------------------------------------------------
suifengjz 2017-04-24
  • 打赏
  • 举报
回复
#include <windows.h> char chpath[MAX_PATH]; GetModuleFileName(NULL,(LPSTR)chpath,sizeof(chpath)); std::cout<<chpath<<std::endl; 出现错误error C2664: “GetModuleFileNameW”: 不能将参数 2 从“LPSTR”转换为“LPWCH”
加载更多回复(7)

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧