Dat文件的输入与输出

本文代码来源于:https://blog.csdn.net/zy19940906/article/details/49659895

#pragma once
#ifndef _STRUCT_H_
#define _STRUCT_H_

#include<vector>
#include<iostream>
#include <string>
using namespace std;
#define  MaxSize 10
#pragma  pack(1)
typedef struct _TEST_DAT
{
    int type;
    //char name[MaxSize];
    int age;
}TESTDAT;
#pragma pack()
#endif
#pragma once
#ifndef _TXT_TO_FILE_H_
#define _TXT_TO_FILE_H_
#include<vector>
#include<iostream>
#include<fstream>
#include<string>
#include"Struct.h"
#include<stdio.h>
using namespace std;
class TxtToFile
{
public:
    TxtToFile();
    ~TxtToFile();
    bool SaveToDat(string _datPath);//外调函数,保存到dat文件
    bool GetFromDat(string _datPath);
private:
    TESTDAT m_dat;
    fstream m_datstream;
    vector<TESTDAT>m_getdat;
private:
    //把基本的Number类型写入文件流中
    template<class T>
    bool Write(T value)
    {
        m_datstream.write(reinterpret_cast<char *>(&value), sizeof(T));
        return true;
    }
    //把字符串写入文件流
    void WriteString(string _str, int _count);

    __int64 getFileSize(const char *filename);
};
#endif
#include "TxtToFile.h"

#include <sstream>


TxtToFile::TxtToFile()
{
    m_dat = TESTDAT{ 123, 345 };
}


TxtToFile::~TxtToFile()
{
}

bool TxtToFile::SaveToDat(string _datPath)
{
    if (_datPath.size() == 0)
    {
        return false;
    }
    m_datstream.open(_datPath, fstream::out | fstream::binary);
    if (!m_datstream.is_open())
    {
        cout << "open" << _datPath << "Failed" << endl;
        m_datstream.close();
        return false;
    }
    Write(m_dat.type);
    //WriteString(m_dat.name, MaxSize);
    Write(m_dat.age);

    cout << "Successed" << endl;
    m_datstream.close();
    return true;
}

void TxtToFile::WriteString(string _str, int _count)
{
    if ((int)_str.length() >= _count)
    {
        cout << "write string error!" << endl;
        return;
    }
    m_datstream << _str;
    int len = _count - _str.length();
    cout << _count << endl;
    cout << _str.length() << endl;
    while (len--)
    {
        m_datstream << "\0";
    }
}

bool TxtToFile::GetFromDat(string _datPath)
{
    if (_datPath.size() == 0)
    {
        return false;
    }
    __int64 scount = 0;
    __int64 stuctsize = sizeof(TESTDAT);
    cout << stuctsize;
    TESTDAT testdat;
    __int64 filesize = getFileSize(_datPath.c_str());
    cout << filesize;
    //if (filesize == 0 || filesize < stuctsize || (filesize%stuctsize) != 0)
    //{
    //  return false;
    //}
    //scount = filesize / stuctsize;
    //cout << scount;
    FILE *inStream = NULL;
    const char *name = _datPath.c_str();
    fopen_s(&inStream,name, "rb");
    if (inStream == NULL)return false;
    for (int i = 0; i < 1; i++)
    {
        fread(&testdat, stuctsize, 1, inStream);
        m_getdat.push_back(testdat);
        cout << testdat.age << " " << testdat.type << " " << endl;
    }
    fclose(inStream);
    return true;
}

__int64 TxtToFile::getFileSize(const char *filename)
{
    __int64 size = 0;
    fstream instreams;
    instreams.open(filename, ios::_Nocreate);
    if (!instreams.is_open())
    {
        return 0;
    }
    instreams.seekg(0, ios::end);//移到最后
    size = instreams.tellg();//告诉现在位置在哪里
    instreams.close();
    return size;
}


#include"TxtToFile.h"

int main()
{
    TxtToFile ttofile;
    ttofile.SaveToDat("Test.Dat");
    ttofile.GetFromDat("Test.Dat");
    getchar();
    getchar();
    return 0;
}

Dat文件是一个存取数据的文件,不管是什么数据我们都是将其转换成char型数据进行的存取,所以在读取出来时,我们必须严格按照存入的格式进行存取,比如我们存储数据为int double float那么我们就必须按照这三种格式进行存取,出现一个字节的偏移都会导致后面的结果全面出错。所以我们用记事本打开时dat文件时总是乱码的原因。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值