Linux开发之C++操作文件及文件夹

12 篇文章 1 订阅

1. 文件读写

1.1. open

● 函数头文件:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

● 函数原型:

int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);

flags常用的有3种,O_RDONLY, O_WRONLY, O_RDWR,分别对应只读、只写和读写。更多信息见手册。

1.2. read

● 函数头文件:

#include <unistd.h>

● 函数原型:

ssize_t read(int fd, void *buf, size_t count);

1.3. write

● 函数头文件:

#include <unistd.h>

● 函数原型:

ssize_t write(int fd, const void *buf, size_t count);

1.4. lseek

● 函数头文件:

#include <sys/types.h>
#include <unistd.h>

● 函数原型:

off_t lseek(int fd, off_t offset, int whence);
whence可又是SEEK_SET、SEEK_CUR、SEEK_END,更多见手册。

1.5. fcntl

● 函数头文件:

#include <unistd.h>
#include <fcntl.h>

● 函数原型:

int fcntl(int fd, int cmd, … /* arg */ );

fcntl主要是设置指定文件的一些控制操作,如写锁、读锁、同步异步等。

1.6. stat

● 函数头文件

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

● 函数原型

int stat(const char *pathname, struct stat *statbuf);
int fstat(int fd, struct stat *statbuf);

获取文件的相关信息,如大小等。

1.7. close

● 函数头文件

#include <unistd.h>

● 函数原型

int close(int fd);

1.8. 示例

#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>

unsigned long get_file_size(const char *path)
{
	unsigned long filesize = -1;	
	struct stat statbuff = {};
	if (stat(path, &statbuff) < 0)
	{
		return filesize;
	}
	else
	{
		filesize = statbuff.st_size;
	}
	return filesize;
}

int main(int argc,char *argv[])
{
	int nSize = get_file_size("./test");
	int fd = open("./test", O_RDWR);
	if (fd < 0)
	{
		return 1;
	}
	char* pBuff = new char[nSize]();
	read(fd, pBuff, nSize);

	lseek(fd, 10, SEEK_SET);
		
	const char* pWBuff = "ABCDEFG";
	write(fd, pWBuff, strlen(pWBuff));

	close(fd);
	delete[] pBuff;
	pBuff = NULL;
}

2. 遍历指定目录下的所有文件

#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include <dirent.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <string.h>
using namespace std;


/* Show all files under dir_name , do not show directories ! */
void showAllFiles(const char *dir_name)
{
	// check the parameter !
	if (NULL == dir_name)
	{
		cout << " dir_name is null ! " << endl;
		return;
	}

	// check if dir_name is a valid dir
	struct stat s = {};
	lstat(dir_name, &s);
	if (!S_ISDIR(s.st_mode))
	{
		cout << "dir_name is not a valid directory !" << endl;
		return;
	}

	struct dirent *filename = NULL;
	DIR* dir = opendir(dir_name);
	if (NULL == dir)
	{
		cout << "Can not open dir " << dir_name << endl;
		return;
	}
	cout << "Successfully opened the dir !" << endl;

	/* read all the files in the dir ~ */
	while ((filename = readdir(dir)) != NULL)
	{
		// get rid of "." and ".."
		if (strcmp(filename->d_name, ".") == 0 ||
			strcmp(filename->d_name, "..") == 0)
			continue;
		cout<<"FileName:"<<filename->d_name << endl;
		cout<<"FilePath:"<<dir_name<<"/"<<filename->d_name << endl;
	}
}

int main(int argc, char *argv[])
{
	// current dir
	showAllFiles(".");
}

3. 递归遍历指定目录所有文件

#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <dirent.h>
#include <vector>
#include <string>


int GetAbsoluteFiles(const std::string& directory, std::vector<std::string>& filesAbsolutePath) //参数1[in]要变量的目录  参数2[out]存储文件名
{
    DIR* dir = opendir(directory.c_str()); //打开目录   DIR-->类似目录句柄的东西
    if ( dir == NULL )
    {
        return -1;
    }

    struct dirent* d_ent = NULL;       //dirent-->会存储文件的各种属性
    char fullpath[128] = {0};
    char dot[3] = ".";                //linux每个下面都有一个 .  和 ..  要把这两个都去掉
    char dotdot[6] = "..";

    while ( (d_ent = readdir(dir)) != NULL )    //一行一行的读目录下的东西,这个东西的属性放到dirent的变量中
    {
        if ( (strcmp(d_ent->d_name, dot) != 0)
              && (strcmp(d_ent->d_name, dotdot) != 0) )   //忽略 . 和 ..
        {
            if ( d_ent->d_type == DT_DIR ) //d_type可以看到当前的东西的类型,DT_DIR代表当前都到的是目录,在usr/include/dirent.h中定义的
            {

                std::string newDirectory = directory + std::string("/") + std::string(d_ent->d_name); //d_name中存储了子目录的名字
                if( directory[directory.length()-1] == '/')
                {
                    newDirectory = directory + std::string(d_ent->d_name);
                }

                if ( -1 == GetAbsoluteFiles(newDirectory, filesAbsolutePath) )  //递归子目录
                {
                    return -1;
                }
            }
            else   //如果不是目录
            {
                std::string absolutePath = directory + std::string("/") + std::string(d_ent->d_name);  //构建绝对路径
                if( directory[directory.length()-1] == '/')  //如果传入的目录最后是/--> 例如a/b/  那么后面直接链接文件名
                {
                    absolutePath = directory + std::string(d_ent->d_name); // /a/b/1.txt
                }
                filesAbsolutePath.push_back(absolutePath);
            }
        }
    }

    closedir(dir);
    return 0;
}


int main(int argc,char *argv[])
{
	std::vector<std::string> vstrFile;
	GetAbsoluteFiles("/", vstrFile);
  for (size_t i = 0; i < vstrFile.size(); i++)
  {
    std::cout<<vstrFile[i].c_str()<<std::endl;
  }
	return 0;
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值