GetCurrentDirectory 理解

来源:互联网 发布:linux工程师 编辑:程序博客网 时间:2024/06/09 15:00

//====================================GetCurrentDirectory============================//
/*
  GetCurrentDirectory它表示当前目录   打开哪就是哪
  文件路径 需要再操作,如C:/windows   不需要处理转义符         strcat(lpStr,"//XXX.txt");

  GetCurrentDirectory只是获取当前的目录,当计算机重新启动后,当前的路径一般为系统目录,因此系统启动后,
  使用该函数只能获取当前的目录,而不是应用程序所在的目录。
  我建议你使用GetModuleFileName函数,GetModuleFileName()是一个得到路径的API 函数。
  DWORD GetCurrentDirectory(_in DWORD nBufferSize , _out LPCSTR lpBuffer);
  lpBuffer return the current process directory.
  returns Can judge the state of the function. returns means the bytes lpBuffer read really,if this function is success.
  returns may stand for the bytes needs for the directory space ,if this function is failed.
  else,you may call the function GetLastError Judge the reason

  the direction may be
*/
#include<iostream>
#include<windows.h>
using namespace std;
#define nSize 60
void main()
{
    CHAR lpStr[nSize];      //allocate the space for the directory
    DWORD nPathSize=0;      //record the returns of the function,which is used to judge the state of the function.
    memset(lpStr,0,nSize);  //space init
    nPathSize = GetCurrentDirectory(nSize,lpStr);
    if(nPathSize<nSize)
       cout<<lpStr<<endl;
    else
        cout<<"need space is "<<nPathSize<<endl;
    system("pause");
}

原创粉丝点击