C++ 文件操作fstream -- filesystem遍历目录

文件打开方式

ios::in   	// 读取的方式打开。如果文件不存在,则打开出错。
ios::out  	//  写入的方式打开,文件不存在就新建一个,文件存在就清除所有内容
ios::app	// 以追加的方式打开文件 不存在 则新建
ios::ate 	 // 追加的方式打开文件	文件不存在则打开出错
ios::trunc   // 打开时清除所有数据
iso::binary  //二进制方式打开文件
ios::nocreate: //不建立文件,所以文件不存在时打开失败
ios::noreplace://不覆盖文件,所以打开文件时如果文件存在失败

文件流操作类

ofstream
	文件流输出
ifstream
	文件流输入

1.文件写入操作

// 入参是文件的路径+名称
void fileWrite(string &name){
    ofstream f(name,ios::app);
    if(!f.eof()) {
		f<<"this is a test text!";
		//f.write("this is a write text!",30);	 
	} 
    f.close();
    return ;
}

2.文件读取操作

// 入参是文件的路径+名称
void fileRead(string &name){
    ifstream f;
    f.open(name,ios::in);
    while(f.good() && !f.eof()){
        string temp;
        getline(f,temp);
        cout<<temp<<endl;
        // read
        char temp2[21];
        f.read(temp2,20);
        cout<<"read content:"<<temp2<<endl;
    }
    f.close();
    return ;
}

3.文件指针移动操作

seekg() tellg() 
seek 设置文件指针的位置 
g get  
p put
seekp() tellp()
void moveFilePointer(string &name){
    ifstream f(name);
    f.seekg(-10,ios::end);  // 移动到文件结尾前
     while(f.good() && !f.eof()){
        string temp;
        getline(f,temp);
        cout<<temp<<endl;
    }
    f.seekg(0,ios::beg); // 移动到文件开头
         while(f.good() && !f.eof()){
        string temp;
        getline(f,temp);
        cout<<temp<<endl;
    }
    f.close();
    return ;
}

4.获取文件指针的位置

void getTellg(string &name){
    ifstream f(name);
    //f.seekg(-100,ios::end);  // 移动到文件结尾前
     while(f.good() && !f.eof()){
        string temp;
        cout<<f.tellg()<<" : ";
        getline(f,temp);
        cout<<temp<<endl;
    }
}

5.计算文件大小

// 返回的单位是Kb
streampos calcFileSize(string &name)  {
    ifstream f(name);
    f.seekg(0,ios::end);
    streampos sp = f.tellg();
    return sp; 
}

6.遍历目录下的所有文件

#include <iostream>
#include <filesystem>
#include <string>

using namespace std;
using namespace std::filesystem;

void circlePath(string &name) {
	path p(name);
	if (!exists(name)) {
		cerr << "文件不存在" << endl;
		return;
	}
	directory_entry entry(name);
	if (file_type::directory == entry.status().type()) {
		directory_iterator list(name);
		for (auto& it : list) {
			if (it.status().type() == file_type::directory) {
				string str = it.path().string();
				circlePath(str);
			}else
				cout << it.path().filename() << endl;
		}	
	}
	return;
}

// 测试
int main()
{
	string str("D:\\Go");
	circlePath(str);
	return 0;

}

7.修改文件名称

void updateFileName(string &name) {
	path p(name);
	if (!exists(name)) {
		cerr << "文件不存在" << endl;	
		return;
	}
	string newName;
	newName+=p.parent_path().string() += p.stem().string() +'2'+p.extension().string();
	string oldName(p.string());
	cout << "oldName:" << oldName << endl;
	cout << "newName:" << newName << endl;
	cout << rename(oldName.c_str(), newName.c_str()) << endl;
}
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值