C++ 文件操作封装成class

//#include "md5.h"
#include <iostream>
#include <malloc.h>
#include <memory.h>
#include <sys/time.h>
#include <stdio.h>
#include <time.h>
#include <cstdlib>
#include <sys/types.h>//这里提供类型pid_t和size_t的定义
#include <sys/stat.h>
#include <fcntl.h> // for open
#include <unistd.h> // for close
#include <string.h>
#include <stdlib.h>
#include <sys/mman.h>


using namespace std;

class file_operation
{
private:
    int fd;
    long size;
public:
    char *data;
    
public:
    file_operation():fd(-1),size(0),data(NULL)
    {
        std::cout << "fd=" << fd << " "<< "size="<< size << std::endl;
    }

    int openfile(std::string filepath)
    {
        fd = open(filepath.c_str(),O_RDWR  |O_CREAT|O_APPEND ,0644);
        if(fd <= 0)
            return -1;
        /*
         * ftruncate()会将参数fd指定的文件大小改为参数length指定的大小。参数fd为已打开的文件描述词,而且必须是以写入模式打开的文件。
         * ftruncate 必须执行在open 之后
         * ftruncate(fd,20);
         */
         
        size = lseek(fd, 0, SEEK_END);//读取文件内容实际长度
        data = (char *) mmap( NULL, size ,PROT_READ | PROT_WRITE, MAP_SHARED , fd, 0 );
        std::cout << "time : " << getDateTime() << std::endl;
        std::cout << "fd=" << fd << " "<< "size="<< size << std::endl;
        return 0;
    }
    /*
     * 仅仅覆盖文件中写文件的长度
     */
    int writefile(std::string context)
    {
        const char *buf = context.c_str();
        if(size < strlen(buf))
            return -1;
        memcpy(data,buf,strlen(buf));
        //只有在调用了munmap()后或者msync()时,才把内存中的相应内容写回磁盘文件,所写内容仍然不能超过文件的大小。
        msync(data,strlen(buf),MS_SYNC|MS_INVALIDATE);
        return 0;
    }
    /*
     * 文件中全部填充为0
     */
    void clearfile(void)
    {
        memset(data,0,size);
        msync(data,size,MS_SYNC|MS_INVALIDATE);
    }
    
    void closefile(void)
    {
        munmap(data,size);
        close(fd);
    }
    
    char* getDateTime()
    {
        static char nowtime[20];
        memset(nowtime,0,20);
        time_t rawtime;
        struct tm* ltime;
        time(&rawtime);
        ltime = localtime(&rawtime);
        strftime(nowtime, 20, "%Y-%m-%d %H:%M:%S", ltime);
        return nowtime;
    }
};

int main()
{
    string filePath = "./test.yuv";
    string md5_str = "42574129ec74f3f5df52e72396142b07";
    string hashBuf;
    file_operation File;
    File.openfile(filePath);
    std::cout << File.getDateTime() << std::endl;
	hashBuf =(char*)(File.data);
	#if 0
	MD5 md5;
	md5.update(hashBuf);
    std::cout << md5.toString() << std::endl;
    if ( md5.toString() == md5_str)
        std::cout << "picture test success!" << std::endl;
    md5.reset();
    std::cout << File.getDateTime() << std::endl;
   // File.clearfile();
    File.writefile("22");
	hashBuf =(char*)(File.data);;
	md5.update(hashBuf);
    std::cout << md5.toString() << std::endl;    
    #endif
    File.closefile();
    return 0;
}
在软件工程中,封装是面向对象编程的一个核心概念,它指的是将数据(或状态)和行为(或功能)捆绑到一起的过程,使得这些数据和行为对外部隐藏起来,只通过一个公开的接口进行访问。封装的主要目的是增加代码的模块性和安全性。 在C和C++中实现封装通常通过以下几种方式: 1. C语言中的封装: 在C语言中,并没有面向对象的特性,所以封装通常是通过结构体(struct)来模拟的。你可以定义一个结构体来表示一个数据集合,然后通过函数来操作这个结构体,实现数据的隐藏和封装的效果。 例如: ```c // 定义一个结构体表示一个人的信息 typedef struct { char name[50]; int age; } Person; // 提供一个函数来创建Person实例 Person create_person(const char* name, int age) { Person p; strncpy(p.name, name, sizeof(p.name)); p.age = age; return p; } // 提供函数来访问Person的私有数据 const char* get_person_name(Person* p) { return p->name; } int get_person_age(Person* p) { return p->age; } ``` 2. C++语言中的封装C++支持面向对象编程,因此可以使用类(class)来实现封装。在C++中,类员默认是私有的(private),只能在类的内部访问。如果要从类的外部访问,可以提供公共员函数(public)作为接口。 例如: ```cpp // 定义一个类表示一个人的信息 class Person { private: std::string name; // C++中推荐使用标准库中的string类型 int age; public: // 构造函数用于创建Person对象 Person(const std::string& name, int age) : name(name), age(age) {} // 提供公共接口来访问私有员 const std::string& get_name() const { return name; } int get_age() const { return age; } }; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值