GetCurrentDirectory 理解

本文介绍如何使用GetCurrentDirectory和GetModuleFileName函数来获取当前工作目录和应用程序所在路径。GetCurrentDirectory仅能获取当前目录,而GetModuleFileName则更为推荐使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//====================================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");
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值