C++ 文件夹目录读取 与 iostream读写文件

文件夹目录读取

int getAbsoluteFiles(string directory, vector<string>& filesAbsolutePath) //参数1[in]要变量的目录  参数2[out]存储文件名
{
    DIR* dir = opendir(directory.c_str()); //打开目录   DIR-->类似目录句柄的东西
    if ( dir == NULL )
    {
        cout<<directory<<" is not a directory or not exist!"<<endl;
        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中定义的
            {

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

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

iostream读写文件

打开方式解释
ios::in为读文件而打开文件
ios::out为写文件而打开文件
ios::ate初始位置:文件尾
ios::app追加方式写文件
ios::trunc如果文件存在先删除,再创建
ios::binary二进制方式

便于记忆:
以代码本身为主体,
向外输出out,即为写文件;
向内读入in,即为读文件。

//文本文件 读文件
#include<iostream>
#include<fstream>
#include<string>

using namespace std;


//文本文件 写文件
void writeFile() {
	//1、包含头文件fstream
	// 2、创建对象流
	ofstream ofs;
	 
	// 3、指定打开方式
	ofs.open("test.txt", ios::out);

	// 4、写文件
	ofs << "姓名:张三 " << endl;
	ofs << "性别:男" << endl;
	ofs << "年龄:18" << endl;

	// 5、关闭文件
	ofs.close();

}

//文本文件 读文件
void readFile() {
	//1、包含头文件fstream
	//2、创建对象流
	ifstream ifs;

	// 3、打开文件,并判断是否打开成功
	ifs.open("test.txt", ios::in);
	if (!ifs.is_open()) {
		cout << "文件打开失败" << endl;
		return;
	}

	//4、读文件
	//法一
	//char buf[1024] = {0};
	//while (ifs >> buf) { // 一次读一行
	//	cout << buf << endl;
	//}

	//法二
	/*char buf[1024] = { 0 };
	while (ifs.getline(buf,sizeof(buf)))
	{
		cout << buf << endl;
	}*/

	// 法三
	string buf;
	while (getline(ifs, buf)) {
		cout << buf << endl;
	}
	
	 法四 (较慢,不提倡)
	//char c;
	//while ((c = ifs.get()) != EOF) {  //EOF = End of File
	//	cout << c;
	//}
}

int main() {
	writeFile();

	readFile();
	return 0;
}

Reference

linux c++ 遍历一个目录下的文件名 (包括子目录的文件名)

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

昼行plus

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值