C++文件操作

C++文件操作

简介

简单封装c++文件操作,当文件路径不存在时,可创建多级目录,文件读写支持自定义结构体,本文采用二进制文件读写操作。

实现

MyFileHelp.h

/************************************************************************/
/* 文件操作帮助类														*/
/************************************************************************/
#pragma once
#include <stdio.h>
#include <iostream>
#include <direct.h>
#include <string>
#include <io.h>

/************************************************************************/
/* 创建目录																*/
/************************************************************************/
void CREATE_DIR(const char *dir)
{
	int m = 0, n;
	std::string str1, str2;
	str1 = dir;
	str2 = str1.substr(0, 2);
	str1 = str1.substr(3, str1.size());
	while (m >= 0)
	{
		m = str1.find('\\');

		str2 += '\\' + str1.substr(0, m);
		//判断该目录是否存在
		n = _access(str2.c_str(), 0); 
		if (n == -1)
		{
			//创建目录文件
			_mkdir(str2.c_str());     
		}

		str1 = str1.substr(m + 1, str1.size());
	}
}

/************************************************************************/
/* 打开文件
parame:_path 目录路径;_name 文件名字;
*/
/************************************************************************/
inline FILE *OPEN_FILE(const char *_path,const char *_name,const char *_mode = "ab+")
{
	//判断目录是否存在,如不存在创建路径
	if (0 != _access(_path, 0))
	{
		CREATE_DIR(_path);
	}
	//拼接文件路径
	char _t_path[200];
	strcpy_s(_t_path,_path);
	strcat_s(_t_path, "\\");
	strcat_s(_t_path, _name);

	FILE *_file;
	fopen_s(&_file,_t_path,_mode);
	return _file;
}

/************************************************************************/
/* 关闭文件																*/
/************************************************************************/
inline bool CLOSE_FILE(FILE *_file)
{
	if (_file!=NULL)
	{
		fclose(_file);
		return true;
	}
	return false;
}

/************************************************************************/
/* 写文件																*/
/************************************************************************/
template <typename T>
inline void WRITE_FILE(FILE *_file,T _parame)
{
	int _size = sizeof(T);
	fwrite(&_parame,sizeof(T),1,_file);
}

/************************************************************************/
/* 读文件																*/
/************************************************************************/
template <typename T>
inline bool READ_FILE(FILE *_file,T &_parame)
{
	int _size = sizeof(T);
	if(fread(&_parame,sizeof(T),1,_file))
	{
		return true;
	}
	return false;
}

测试

main.cpp

#include <iostream>
#include "MyFileHelp.h"
using namespace std;

struct Student
{
	string _id;
	string _name;
	int _age;
};

int main()
{
	//打开文件
	FILE *_file = OPEN_FILE("D:\\first\\second","hello.dat");
	Student _student;
	_student._id = "123456";
	_student._name = "小明";
	_student._age = 18;
	//写入文件
	WRITE_FILE(_file,_student);
	_student._id = "234567";
	_student._name = "小红";
	_student._age = 19;
	WRITE_FILE(_file,_student);
	//关闭文件(在读文件时,要提前关闭文件)
	CLOSE_FILE(_file);

	Student _student_1;
	_file = OPEN_FILE("D:\\first\\second","hello.dat");
	//读文件读取文件中所有内容,输出至控制台
	while(READ_FILE(_file,_student_1))
	{
		cout<<"学号:"<<_student_1._id<<endl;
		cout<<"姓名:"<<_student_1._name<<endl;
		cout<<"年龄:"<<_student_1._age<<endl;
		cout<<"----------------"<<endl;
	}
	CLOSE_FILE(_file);

	system("pause");
	return 0;
}

结果

控制台显示
在这里插入图片描述
生成文件路径
在这里插入图片描述
二进制文件内容

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

糖儿糖儿

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

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

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

打赏作者

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

抵扣说明:

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

余额充值