C++ 读文件示例代码

本文介绍了一个用于处理CSV文件的C++类CReadPFfromCSV的实现细节,包括从CSV文件加载数据到内存、保存数据回文件的功能,并提供了获取整数、浮点数和字符串值的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 .CReadPFfromCSV.h

#pragma once  
//#include "stringparser.h"  
#include <assert.h>  
#include <map>  
#include <vector>  
#include <string> 
#include<fstream>
#include<iostream>
#include<iomanip>
#include <string>
#include <cstring>
#include <sstream>
//#include "SkillRecord.h"  
using namespace std; 

typedef unsigned long   u32; 

class CReadPFfromCSV
{
private:  
    map<u32, map<u32, string>> m_stringMap;  
    string m_CSVName; 


public:
    CReadPFfromCSV(void);

    CReadPFfromCSV(const char *path)  
    {  
        assert(LoadCSV(path));  
    }  

    bool LoadCSV(string path);  
    bool Loadthrmldata(const char *path);
    bool SaveCSV(const char *path = NULL);  

    bool GetIntValue(u32 uiRow, u32 uiCol, int &riValue);  
    bool GetFloatValue(u32 uiRow, u32 uiCol, float &rfValue);  
    string* GetStringValue(u32 uiRow, u32 uiCol);  

    int GetParamFromString(string str, vector<string> &stringVec, char delim  = ',');  
    int GetParamFromStringb(string str, vector<string> &stringVec, char delim  = ' ');  


    map<u32, map<u32, string>>& GetCSVMap()  
    {  
        return m_stringMap;  
    }  

    ~CReadPFfromCSV(void);
};

2. CReadPFfromCSV.cpp

#include "stdafx.h"
#include "ReadPFfromCSV.h"

bool CReadPFfromCSV::LoadCSV(string strpath)  
{  
    const char *path = strpath.c_str();
    FILE *pFile;
    int err = fopen_s(&pFile,path, "r");  

    if (pFile)  
    {  
        //read stream from file
        ifstream in(path, ios::in);
        istreambuf_iterator<char> beg(in), end;
        string strdata(beg, end);
        in.close();

        //convert from string to char[]
        const char*cfirst = strdata.c_str();
        char*fileBuffer = new char[strlen(cfirst) + 1];
        strcpy( fileBuffer, cfirst);

        map<u32, string> stringMap;  
        u32 uiIndex = 1;  
        char *pBegin = fileBuffer;  
        char *pEnd = strchr(pBegin, '\n');  


        pBegin = pEnd + 1;  
        pEnd = strchr(pBegin, '\n');
        pBegin = pEnd + 1;  
        pEnd = strchr(pBegin, '\n');  

        while (pEnd)  
        {  
            string strTemp;  
            strTemp.insert(0, pBegin, pEnd-pBegin);  
            assert(!strTemp.empty());  
            stringMap[uiIndex++] = strTemp;  
            pBegin = pEnd + 1;  
            pEnd = strchr(pBegin, '\n');  

            //cout<<strTemp<<endl;
            //cout<<stringMap.size()<<endl;
        }  
        delete []fileBuffer;  
        fileBuffer = NULL;  
        pBegin = NULL;  
        pEnd = NULL;  
        //cout<<stringMap.size()<<endl;


        //read every element in each line to map:stringMapTemp and to vector
        map<u32, string>::iterator iter = stringMap.begin();  
        for (; iter != stringMap.end(); ++iter)  
        {  
            vector<string> stringVec;  
            map<u32, string> stringMapTemp;  
            assert(GetParamFromString(iter->second, stringVec) > 0);  

            vector<string>::size_type idx = 0;  
            for (; idx != stringVec.size(); ++idx)  
            {  
                stringMapTemp[idx + 1] = stringVec[idx];  
            }  

            m_stringMap[iter->first] = stringMapTemp;  
        }  

        fclose(pFile);  
        m_CSVName = path;  
        return true;  
    }   
    else  
    {  
        return false;  
    }  
}  



//Function: Loadthrmldata(const char *path)
//          load the data about the thermal limit
//Input : file named violmt.dat
//        
//Result: the map contains the data about the lines
bool CReadPFfromCSV::Loadthrmldata(const char *path)
{
    FILE *pFile = fopen(path, "r");  

    if (pFile == NULL)
    {
        cout<<"violmt.dat is not found."<<endl;
    }

    if (pFile)  
    {  
        //read stream from file
        ifstream in(path, ios::in);
        istreambuf_iterator<char> beg(in), end;
        string strdata(beg, end);
        in.close();

        //convert from string to char[]
        const char*cfirst = strdata.c_str();
        char*fileBuffer = new char[strlen(cfirst) + 1];
        strcpy( fileBuffer, cfirst);

        map<u32, string> stringMap;  
        u32 uiIndex = 1;  
        char *pBegin = fileBuffer;  
        char *pEnd = strchr(pBegin, '\n');  

        //skip the data about the voltage
        while (pEnd)
        {
            string strTemp;  
            strTemp.insert(0, pBegin, pEnd-pBegin); 
            //extract the data
            //next row
            pBegin = pEnd + 1;  
            pEnd = strchr(pBegin, '\n'); 
            //zeros row
            if (strTemp.size()<8)
            {
                break;
            }
        }

        //thermal limit part
        while (pEnd)  
        {  
            string strTemp;  
            strTemp.insert(0, pBegin, pEnd-pBegin);  
            assert(!strTemp.empty());  
            stringMap[uiIndex++] = strTemp;  
            pBegin = pEnd + 1;  
            pEnd = strchr(pBegin, '\n');  

            //cout<<strTemp<<endl;
            //cout<<stringMap.size()<<endl;
        }  
        delete []fileBuffer;  
        fileBuffer = NULL;  
        pBegin = NULL;  
        pEnd = NULL;  
        //cout<<stringMap.size()<<endl;


        //read every element in each line to map:stringMapTemp and to vector
        map<u32, string>::iterator iter = stringMap.begin();  
        for (; iter != stringMap.end(); ++iter)  
        {  
            vector<string> stringVec;  
            map<u32, string> stringMapTemp;  

            GetParamFromStringb(iter->second, stringVec,' ');
            //assert(GetParamFromStringb(iter->second, stringVec,' ') > 0);  

            vector<string>::size_type idx = 0;  
            for (; idx != stringVec.size(); ++idx)  
            {  
                stringMapTemp[idx + 1] = stringVec[idx]; 
                //cout<<stringVec[idx]<<endl;
            }  

            m_stringMap[iter->first] = stringMapTemp;  
        }  

        fclose(pFile);  
        m_CSVName = path;  
        return true;  
    }   
    else  
    {  
        return false;  
    }  
}

bool CReadPFfromCSV::SaveCSV(const char *path /* = NULL */)  
{  
    if (path != NULL)  
    {  
        m_CSVName = path;  
    }  

    FILE *pFile = fopen(m_CSVName.c_str(), "w");  
    if (pFile)  
    {  
        map<u32, map<u32, string>>::iterator iter = m_stringMap.begin();  
        for (; iter != m_stringMap.end(); ++iter)  
        {  
            map<u32, string> &rStringMap = iter->second;  
            map<u32, string>::iterator it = rStringMap.begin();  
            for (; it != rStringMap.end(); ++it)  
            {  
                string strTemp = it->second;  
                strTemp += ',';  
                fwrite(strTemp.c_str(), 1, 1, pFile);  
            }  

            char delim = '\n';  
            fwrite(&delim, 1, 1, pFile);  
        }  

        fclose(pFile);  
        return true;  
    }   
    else  
    {  
        return false;  
    }  
}  

bool CReadPFfromCSV::GetIntValue(u32 uiRow, u32 uiCol, int &riValue)  
{  
    string *pStr = GetStringValue(uiRow, uiCol);  
    if (pStr)  
    {  
        riValue = atoi(pStr->c_str());  
        return true;  
    }   
    else  
    {  
        return false;  
    }  
}  

bool CReadPFfromCSV::GetFloatValue(u32 uiRow, u32 uiCol, float &rfValue)  
{  
    string *pStr = GetStringValue(uiRow, uiCol);  
    if (pStr)  
    {  
        rfValue = atof(pStr->c_str());  
        return true;  
    }   
    else  
    {  
        return false;  
    }  
}  

string* CReadPFfromCSV::GetStringValue(u32 uiRow, u32 uiCol)  
{  
    map<u32, map<u32, string>>::iterator iter = m_stringMap.find(uiRow);  
    if (iter != m_stringMap.end())  
    {  
        map<u32, string> &rStrMap = iter->second;  
        map<u32, string>::iterator it = rStrMap.find(uiCol);  
        if (it != rStrMap.end())  
        {  
            return &(it->second);  
        }   
        else  
        {  
            return NULL;  
        }  
    }   
    else  
    {  
        return NULL;  
    }  
}  

//用于分割字符串,将CSV表格中的一行按照规则解析成一组字符串,存储在一个vector中  
//根据CSV表格中所存储的数据的不同,重载各函数  
int CReadPFfromCSV::GetParamFromString(string str, vector<string> &stringVec, char delim)  
{  
    char *token = strtok(const_cast<char *>(str.c_str()), &delim);  
    while (token)  
    {  
        string strTemp = token;  
        stringVec.push_back(strTemp);  
        token = strtok(NULL, &delim); 
        //cout<<strTemp<<endl;
    }  

    return stringVec.size();  
}  

//用于分割字符串,将CSV表格中的一行按照规则解析成一组字符串,存储在一个vector中  
//根据CSV表格中所存储的数据的不同,重载各函数  
int CReadPFfromCSV::GetParamFromStringb(string str, vector<string> &stringVec, char delim)  
{  
    char *token = strtok(const_cast<char *>(str.c_str()), &delim);  
    while (token)  
    {  
        string strTemp = token;  
        stringVec.push_back(strTemp);  
        token = strtok(NULL, &delim); 
        //cout<<strTemp<<endl;
    }  

    return stringVec.size();  
}  
/*
void CReadPFfromCSV::GetSkillRecordMapTable(map<int, SkillRecord> &sSkillMapTable)  
{  
    map<u32, map<u32, string>>::iterator iter = m_stringMap.begin();  
    for (; iter != m_stringMap.end(); ++iter)  
    {  
        map<u32, string> strmap = iter->second;  
        SkillRecord skillTemp;  
        skillTemp.SetID(atoi(strmap[1].c_str()));  
        skillTemp.SetPath(strmap[2]);  
        skillTemp.SetName(strmap[3]);  
        skillTemp.SetHurt(atoi(strmap[4].c_str()));  
        skillTemp.SetPlayTime(atoi(strmap[5].c_str()));  

        sSkillMapTable[skillTemp.GetID()] = skillTemp;  
    }  
}  
*/

CReadPFfromCSV::CReadPFfromCSV(void)
{
}


CReadPFfromCSV::~CReadPFfromCSV(void)
{
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值