获取文件当前目录
2013-04-27 16:54
134 查看
1. GetCurrentDirectory
此函数是微软Windows 提供的。
头文件为WinBase.h
简单来段代码
2._getcwd
此函数头文件为
#include <direct.h>
简单代码:
两个结果一致。
有篇博客说用微软的函数路径中间不能加 “.”,测试了一下,我的电脑上的可以的。表示奇怪。
来个真相:
msdn帮助文档自己可以找,有事例代码的。贴过来,需要的可以看看头的注释部分。
不足之处,多多指教!
此函数是微软Windows 提供的。
头文件为WinBase.h
#ifdef UNICODE #define GetCurrentDirectory GetCurrentDirectoryW #else #define GetCurrentDirectory GetCurrentDirectoryA #endif // !UNICODE
简单来段代码
#include <windows.h> std::wstring GetCurrentDirws() { wchar_t buf[1000]; GetCurrentDirectory(1000, buf); wstring strPath = buf; return strPath; }
2._getcwd
此函数头文件为
#include <direct.h>
简单代码:
std::string GetCurrentDirs() { char* buffer; string m_sRe; if( (buffer = _getcwd( NULL, 0 )) == NULL ) { perror( "_getcwd error" ); } else{ m_sRe = buffer; free(buffer); } return m_sRe; }
两个结果一致。
有篇博客说用微软的函数路径中间不能加 “.”,测试了一下,我的电脑上的可以的。表示奇怪。
来个真相:
msdn帮助文档自己可以找,有事例代码的。贴过来,需要的可以看看头的注释部分。
// crt_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. Passing NULL as the buffer forces getcwd to allocate // memory for the path, which allows the code to support file paths // longer than _MAX_PATH, which are supported by NTFS. #include <direct.h> #include <stdlib.h> #include <stdio.h> int main( void ) { char* buffer; // Get the current working directory: if( (buffer = _getcwd( NULL, 0 )) == NULL ) perror( "_getcwd error" ); else { printf( "%s \nLength: %d\n", buffer, strnlen(buffer) ); free(buffer); } }
不足之处,多多指教!
相关文章推荐
- MFC获取当前运行文件的目录方式
- JavaScript获取当前运行脚本文件所在目录的方法
- JavaScript获取当前运行脚本文件所在目录的方法
- node.js认识学习二:__dirname获取当前模块文件所在目录的完整绝对路径
- 获取文件当前目录方法
- jsp编程获取当前目录下的文件和目录及windows盘符的方法
- 在指定路径创建文件夹, 获取EXE文件的当前目录
- Windows命令行获取当前bat文件所在目录,添加永久系统环境变量的方法
- jsp获取当前目录下的文件和目录,获取windows盘符
- pyinstaller 生成单一的EXE文件之后获取当前目录的方法
- Qt学习笔记,使用QDir获取当前目录下所有目录和文件信息
- GetCurrentDirectory获取当前目录而不是当前执行文件的目录
- bat批处理获取当前bat文件目录中所有的ocx dll文件路径
- 获取当前文件目录shell
- Python获取当前目录下所有文件的绝对路径并存储在文件中
- Qt学习笔记,使用QDir获取当前目录下所有目录和文件信息
- Linux下获取当前进程的执行文件的绝对路径和所在目录(通过/proc/self/exe链接)
- Python基础 —— 获取当前文件所在目录,遍历当前目录,获取指定后缀的文件
- PHP获取当前文件所在目录的父目录的路径(两种方法)