您的位置:首页 > 运维架构 > Linux

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

2017-10-14 11:18 573 查看

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

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

为什么在Linux下man不到呢?

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

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

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

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

C++实现Linux和Windows下遍历指定目录下的文件

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

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

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

(Windows VS)_findfirst, _findnext, _findclose
(Linux\Unix)opendir, readdir, closedir

#include <stdio.h>  
#include <sys/types.h>  
#include <dirent.h>  
  
int main()  
{  
    DIR* pDir;  
    struct dirent* ptr;  
    if( !(dir = opendir(".")) )  
        return -1;  
    while( (ptr = readdir(pDir)) != 0 )  
    {  
        /*Processing File*/  
    }  
    closedir(pDir);  
    return 0;  
}  

C/C++获取文件夹下所有文件名
windows和linux通用

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

int main(void)  
{  
    char current_address[100];  
    memset(current_address, 0, 100);  
    getcwd(current_address, 100); //获取当前路径  
    cout<<current_address<<endl;  
    strcat(current_address, "\\*");  
  
    vector<string> files=getFiles((string)current_address);  
    for (int i=0; i<files.size(); i++)  
    {  
        cout<<files[i]<<endl;  
    }  
  
    //cout<<"Hello World"<<endl;  
  
    cout<<"end..."<<endl;  
    cin.get();  
    return 0;  
}  

 

int main(void)  
{  
    DIR *dir;  
    char basePath[100];  
  
    ///get the current absoulte path  
    memset(basePath, '\0', sizeof(basePath));  
    getcwd(basePath, 999);  
    printf("the current dir is : %s\n",basePath);  
  
       
    cout<<endl<<endl;  
    vector<string> files=getFiles(basePath);  
    for (int i=0; i<files.size(); i++)  
    {  
        cout<<files[i]<<endl;  
    }  
  
  
    cout<<"end..."<<endl<<endl;  
    return 0;  
}  

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

获取当前绝对路径getcwd():
windows环境下头文件:

#include <direct.h>

函数说明:

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

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

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

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

linux环境下头文件:

#include <unistd.h>

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

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

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

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

findfile用法

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

C++查找文件函数FindFirstFile

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: