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;
}
结果
控制台显示
生成文件路径
二进制文件内容