Unix/Linux C++应用开发-Linux下文件管理

来源:互联网 发布:rs-100数据盒 编辑:程序博客网 时间:2024/06/05 15:17

Linux编程】C/C++获取目录下文件或目录及linuxfork()函数详解(原创!!实例讲解)

 io.h不是c的标准库么?

为什么在Linuxman不到呢?

重要的是,在linux下能不能使用io.h中的API呢?怎么用?

Linux C读取文件夹下所有文件(包括子文件夹)的文件名

C/C++遍历文件夹和文件

本方法可用于windowsLinux双平台,采用C/C++标准库函数。

C++实现LinuxWindows下遍历指定目录下的文件

C/C++遍历目录下的文件或指定文件

每次遇到这样的问题总会折腾很久,到网上搜,或者查资料,弄了很多次,但就是没记住,这次写程序又遇到了,干脆就把它都弄清楚了,然后顺便在这里记录一下,以后再遇到就不用到处去找了。

        C/C++遍历目录文件主要有两种方式,分别对应在 Windows VS环境下和 Linux\Unix环境下的方法,它们各自所使用的函数如下:

  1. (Windows VS)_findfirst, _findnext, _findclose
  2. (Linux\Unix)opendir, readdir, closedir
  1. #include <stdio.h>  
  2. #include <sys/types.h>  
  3. #include <dirent.h>  
  4.   
  5. int main()  
  6. {  
  7.     DIR* pDir;  
  8.     struct dirent* ptr;  
  9.     if( !(dir = opendir(".")) )  
  10.         return -1;  
  11.     while( (ptr = readdir(pDir)) != 0 )  
  12.     {  
  13.         /*Processing File*/  
  14.     }  
  15.     closedir(pDir);  
  16.     return 0;  
  17. }  

C/C++获取文件夹下所有文件名 windowslinux通用

实现:获取当前目录下的文件名:

  1. int main(void)  
  2. {  
  3.     char current_address[100];  
  4.     memset(current_address, 0, 100);  
  5.     getcwd(current_address, 100); //获取当前路径  
  6.     cout<<current_address<<endl;  
  7.     strcat(current_address, "\\*");  
  8.   
  9.     vector<string> files=getFiles((string)current_address);  
  10.     for (int i=0; i<files.size(); i++)  
  11.     {  
  12.         cout<<files[i]<<endl;  
  13.     }  
  14.   
  15.     //cout<<"Hello World"<<endl;  
  16.   
  17.     cout<<"end..."<<endl;  
  18.     cin.get();  
  19.     return 0;  
  20. }  

 

  1. int main(void)  
  2. {  
  3.     DIR *dir;  
  4.     char basePath[100];  
  5.   
  6.     ///get the current absoulte path  
  7.     memset(basePath, '\0'sizeof(basePath));  
  8.     getcwd(basePath, 999);  
  9.     printf("the current dir is : %s\n",basePath);  
  10.   
  11.        
  12.     cout<<endl<<endl;  
  13.     vector<string> files=getFiles(basePath);  
  14.     for (int i=0; i<files.size(); i++)  
  15.     {  
  16.         cout<<files[i]<<endl;  
  17.     }  
  18.   
  19.   
  20.     cout<<"end..."<<endl<<endl;  
  21.     return 0;  
  22. }  

C\C++获取当前路径(windows

获取当前绝对路径getcwd()

windows环境下头文件:

  1. #include <direct.h>

函数说明:

    函数原型:char*getcwd(char* buffer, int len);

    参数:buffer是指将当前工作目录的绝对路径copybuffer所指的内存空间, lenbuffer的长度。

    返回值:获取成功则返回当前工作目录(绝对路径),失败则返回false(即NULL)。 

    该函数所属头文件为<direct.h>

linux环境下头文件:

  1. #include <unistd.h>

C/C++判断文件夹是否存在以及创建、删除文件夹 windows以及linux通用

windows下批量读取文件夹及子文件夹下的文件名字,方便制作训练样本

C++】遍历文件夹下的图片文件,并返回其路径

利用FindFirstFile(),FindNextFile()函数历遍指定目录的所有文件

findfile用法

Windows API一日一练(58)FindFirstFile和FindNextFile函数

C++查找文件函数FindFirstFile