• 存储本地txt文件——巧妙做法(内存拷贝)


    最近在写一个读写本地txt文件的时候, 遇到了一个麻烦,因为之前vc项目,上面没有问题,

    移到新项目工程的时候,突然CString无法使用,加入afx.h 又和 不能重复包含 windows.h 头文件

    之前的做法是:

     1 class ParamClass
     2 {
     3 public:
     4 
     5     int m_1;
     6     int m_2;
     7     int m_3;
     8     int m_4;
     9     int m_5;
    10     int m_6;
    11 
    12     bool WriteToFile(string str_current_model)
    13     {
    14         string file_name = str_current_model + "\param01.txt";
    15         CString file_name1 = file_name.c_str();
    16 
    17         CStdioFile file1;
    18         if (!file1.Open(file_name1, CStdioFile::modeCreate | CStdioFile::modeWrite, NULL))
    19             return false;
    20         
    21         CString val;
    22         val.Format("%d
    ", m_1);
    23         file1.WriteString(val);
    24         val.Format("%d
    ", m_2);
    25         file1.WriteString(val);
    26         val.Format("%d
    ", m_3);
    27         file1.WriteString(val);
    28         val.Format("%d
    ", m_4);
    29         file1.WriteString(val);
    30         val.Format("%d
    ", m_5);
    31         file1.WriteString(val);
    32         val.Format("%d
    ", m_6);
    33         file1.WriteString(val);
    34 
    35         file1.Close();
    36 
    37         return true;
    38     }
    39 
    40     bool ReadFromFile(string str_current_model)
    41     {
    42         string file_name = str_current_model + "\param01.txt";
    43         CString file_name1 = file_name.c_str();
    44 
    45         CStdioFile file1;
    46         if (!file1.Open(file_name1, CStdioFile::modeRead | CStdioFile::shareExclusive, NULL))
    47             return false;
    48 
    49         CString val[6];
    50         int i = 0;
    51         while (i < 6 && file1.ReadString(val[i++]))
    52         {
    53             
    54         }
    55 
    56         m_1 = atoi(val[0]);
    57         m_2 = atoi(val[1]);
    58         m_3 = atoi(val[2]);
    59         m_4 = atoi(val[3]);
    60         m_5 = atoi(val[4]);
    61         m_6 = atoi(val[5]);
    62 
    63         file1.Close();
    64 
    65         return true;
    66     }
    67 };

    因为比较着急,所以想着直接用cpp的库函数来写,百度找了找,拼凑起来以后,是下面这样的,

     1 #pragma once
     2 //craigtao 2018-8-2
     3 
     4 #include <iostream>
     5 #include<fstream>  
     6 #include<string>
     7 
     8 using namespace std;
     9 
    10 class CModelParam0x
    11 {
    12 public:
    13 
    14     string m_sCurrExePath;
    15     string m_sCurrModel;
    16 
    17     int m_1;
    18     int m_2;
    19     int m_3;
    20     int m_4;
    21     int m_5;
    22 
    23     bool _write_2_file(string path)
    24     {
    25         ofstream of(str_current_path, ios::out);
    26         of << m_1 << endl;
    27         of << m_2 << endl;
    28         of << m_3 << endl;
    29         of << m_4 << endl;
    30         of << m_5;
    31 
    32         of.close();
    33 
    34         return true;
    35     }
    36 
    37     bool _read_from_file(string path)
    38     {
    39         int temp1, m;
    40         string s;
    41         char buffer[30];
    42         ifstream fin;
    43 
    44         fin.open(str_current_path, ios::in);
    45         if (!fin.is_open()) {
    46             cout << "Error opening file"; exit(1);
    47         }
    48         
    49         int val[5];
    50         int i = 0;
    51 
    52         while (!fin.eof()) {
    53             std::getline(fin, s);
    54             m = atoi(s.substr(0, 6).c_str());
    55             val[i++] = m;
    56         }
    57 
    58         fin.clear();
    59         fin.close();
    60 
    61         m_1 = val[0];
    62         m_2 = val[1];
    63         m_3 = val[2];
    64         m_4 = val[3];
    65         m_5 = val[4];
    66 
    67         return true;
    68     }
    69 };

    以为这样很简洁了,没想到还有这么一招,看了这一招以后,对自己的计算机素养感到很惭愧,

    1 struct Param
    2 {
    3     int m_1;
    4     int m_2;
    5     int m_3;
    6 };

    Param m_Param;
    1 FILE *pFile = fopen("para.txt", "rb");
    2 if (pFile) {
    3    BYTE BUff[512] = "";
    4    fread(BUff, sizeof(Param), 1, pFile);
    5    memcpy(&m_Param, BUff, sizeof(Param));
    6    fclose(pFile);
    7 }
    1 FILE *pFile = fopen("para.txt", "wb");
    2 if (pFile) {
    3     fwrite(&m_Param, sizeof(Param), 1, pFile);
    4     fclose(pFile);
    5 }

    这样写入到txt文件中的内容,使用记事本直接打开的话,是一些字符,不是显示代码中实际的值

    及简洁又快速,佩服,

    希望看到博文的朋友能分享自己使用的一些方法,god bless you!

  • 相关阅读:
    python练习1--求和
    python--文件操作
    【省选】SDOI2017_树点涂色_LuoguP3703/LOJ2001_LCT/线段树
    【洛谷】P1784 数独
    【洛谷】P2671 求和
    【洛谷】P2261 [CQOI2007]余数求和
    【转载】利用向量积(叉积)计算三角形的面积和多边形的面积
    【洛谷】P1090 合并果子
    【转载】学习C++ -> 类(Classes)的定义与实现
    【洛谷】P2142 高精度减法
  • 原文地址:https://www.cnblogs.com/craigtao/p/9414008.html
Copyright © 2020-2023  润新知