A C + + class that operates in CVS format

Source: Internet
Author: User
Tags sprintf strtok

Often need to use Excel, or put some data in Excel open, the program can generate CVS format files, so that Excel opened and processed, and then found a processing CVS C + + class to share with you

The source code cannot be found:

The code is as follows:

StringParser.h

#pragma once#include <process.h> #include <Windows.h> #include <map> #include <vector> #include <queue> #include <set> #include <string> #include <list>typedef char i8;typedef un                Signed Char u8;typedef short i16;typedef unsigned short u16;typedef long int I32;typedef unsigned long u32;namespace stringparser{//get data from the delimiter inline int getparamfromstring (std::string     STR, std::vector<i32>& intvec, char Delim = ', ') {char* p = strtok ((char*) str.c_str (), &delim);        while (p) {intvec.push_back (Atoi (p));     p = strtok (NULL, &delim); } return Intvec.size ();} inline int getparamfromstring (std::string str, std::vector<float>& floatvec, char Delim = ', ') {char* p = Str     Tok ((char*) str.c_str (), &delim);        while (p) {floatvec.push_back (Atof (p));     p = strtok (NULL, &delim); } return Floatvec.size ();}inline int getparamfromstring (std::string Str, std::vector<u32>& uiintvec, char Delim = ', ') {char* p = Strto     K ((char*) str.c_str (), &delim);        while (p) {uiintvec.push_back (Strtoul (p, NULL, 10));     p = strtok (NULL, &delim); } return Uiintvec.size ();} inline int getparamfromstring (std::string Str, std::vector<std::string>& stringvec, char Delim = ', ') {char*     p = strtok ((char*) str.c_str (), &delim);        while (p) {std::string buffer = p;        Stringvec.push_back (buffer);     p = strtok (NULL, &delim); } return Stringvec.size ();} The data in parentheses is obtained by the left and right symbols ex:[3.1415;0.125][1000;9999]template<typename t>int getparamfromarea (std::string Str, std:: Vector<std::vector<t> >& Intvec, char left = ' [', char right = '] ', char Delim = '; ')    {char* Ptarget = (char*) str.c_str ();    for (;;)        {char* Pleft = STRCHR (Ptarget, left);        char* pright = STRCHR (Ptarget, right); if (Pleft && pright) {std::string strbuff;            Strbuff.insert (0, ++pleft, pright-pleft);            Std::vector<t> Intbuff;            if (getparamfromstring (Strbuff, Intbuff, Delim)) {intvec.push_back (Intbuff);        } ptarget = ++pright;        } else {break; }} return Intvec.size ();}};


CCSVOperator.h

#pragma once#include "StringParser.h" class Ccsvoperator{public:    Ccsvoperator () {};    ~ccsvoperator () {};    Ccsvoperator (const char* path);    BOOL Loadcsv (const char* path);    BOOL Savecsv (const char* path = NULL);    BOOL GetInt (u32 uiline, u32 uirow, int& ivalue);    BOOL GetFloat (u32 uiline, u32 uirow, float& fvalue);    std::string* GetString (u32 uiline, u32 uirow);    BOOL Setnumber (u32 uiline, u32 uirow, int ivalue);    BOOL Setnumber (u32 uiline, u32 uirow, float fvalue);    BOOL SetString (u32 uiline, u32 uirow, const char* PSTR);    Std::map<u32, Std::map<u32, std::string> >& Getcsvmap () {return m_stringkeymap;} Protected:    std::string m_csvname;    Std::map<u32, std::map<u32, std::string> > M_stringkeymap;public:int indexoflines;//number of rows int IndexOfColumn ;//number of columns, there may be a different column length of the case};


CSVOperator.cpp

#include "CSVOperator.h"////////////////////////////////////////////////////////////////////////////csv Operatorccsvoperator::ccsvoperator (const char* path) {loadcsv (path);}    BOOL Ccsvoperator::loadcsv (const char* path) {indexoflines = 0; indexofcolumn = 0;    file* pfile = fopen (path, "R");        if (pfile) {fseek (pfile,0,seek_end);        U32 dwsize = Ftell (pfile);        Rewind (pfile);//pointer back to the beginning of the file char* Filebuffer = new Char[dwsize];        Fread (Filebuffer, 1, dwsize, pfile);        Std::map<u32, std::string> Stringmap;        char* pbegin = Filebuffer;        char* pEnd = STRCHR (filebuffer, ' \ n ');//Find the first occurrence of line break u32 uiindex = 1;            while (pEnd! = NULL) {std::string strbuff;            Strbuff.insert (0, Pbegin, pend-pbegin);            if (!strbuff.empty ()) {Stringmap[uiindex] = Strbuff;            } pbegin = pEnd + 1;            PEnd = STRCHR (pEnd + 1, ' \ n ');        ++uiindex; }indexoFlines = uiIndex-1;        Delete[] Filebuffer;        Std::map<u32, Std::string>::iterator iter = Stringmap.begin ();            for (; ITER! = Stringmap.end (); ++iter) {std::vector<std::string> Stringvec;            Std::map<u32, std::string> L_stringmap; Stringparser::getparamfromstring (Iter->second, Stringvec); if (indexofcolumn< stringvec.size ()) {                Indexofcolumn = Stringvec.size ();//save maximum number of columns} for (int i = 0; i < stringvec.size (); ++i) {            L_stringmap[i+1] = stringvec.at (i);        } M_stringkeymap[iter->first] = L_stringmap;        } fclose (Pfile);        M_csvname = path;    return true; } return false;}    BOOL Ccsvoperator::getint (u32 uiline, u32 uirow, int& ivalue) {std::string* PKey = GetString (Uiline, Uirow);        if (pKey) {ivalue = Atoi (Pkey->c_str ());    return true;    } else {return false; }}bool Ccsvoperator::getfloAt (u32 Uiline, u32 uirow, float& fvalue) {std::string* PKey = GetString (Uiline, Uirow);        if (pKey) {fvalue = Atof (Pkey->c_str ());    return true;    } else {return false; }}std::string* ccsvoperator::getstring (u32 uiline, u32 uirow) {std::map<u32, std::map<u32, std::string>;:    : Iterator Iterline = M_stringkeymap.find (uiline);        if (iterline! = M_stringkeymap.end ()) {std::map<u32, std::string>& rstringmap = iterline->second;        Std::map<u32, Std::string>::iterator iterrow = Rstringmap.find (Uirow);        if (Iterrow! = Rstringmap.end ()) {return &iterRow->second;        } else {return NULL;    }} else {return NULL;    }}bool Ccsvoperator::setnumber (u32 uiline, u32 uirow, int ivalue) {std::string* PKey = GetString (Uiline, Uirow);        if (pKey) {char buffer[100];        memset (buffer, 0, sizeof (buffer)); Sprintf (buffer, "%d", ivalue);        Pkey->clear ();        *pkey = buffer;    return true;    } else {return false;    }}bool Ccsvoperator::setnumber (u32 uiline, u32 uirow, float fvalue) {std::string* PKey = GetString (Uiline, Uirow);        if (pKey) {char buffer[100];        memset (buffer, 0, sizeof (buffer));        sprintf (buffer, "%d", fvalue);        Pkey->clear ();        *pkey = buffer;    return true;    } else {return false;    }}bool ccsvoperator::setstring (u32 uiline, u32 uirow, const char* pStr) {std::string* PKey = GetString (Uiline, Uirow);        if (PKey) {pkey->clear ();        *pkey = PSTR;    return true;    } else {return false;    }}bool ccsvoperator::savecsv (const char* path) {if (path! = NULL) {m_csvname = path;    } file* pfile = fopen (M_csvname.c_str (), "w"); if (pfile) {std::map<u32, std::map<u32, std::string> >::iterator iter = M_stringkeyMap.begin (); for (; ITER! = M_stringkeymap.end (); ++iter) {std::map<u32, std::string>& rstringmap = iter-&            Gt;second;            Std::map<u32, Std::string>::iterator it = Rstringmap.begin ();                for (; it! = Rstringmap.end (); ++it) {std::string key = it->second;                Key + = ', ';            Fwrite (Key.c_str (), 1, Key.size (), pfile);            } char Delim = ' \ n ';        Fwrite (&delim, 1, 1, pfile);    } fclose (Pfile);    } else {return false; } return true;


Cvs_op. Cpp

Csv_op.cpp: Defines the entry point of the console application. #include "stdafx.h" #include "CSVOperator.h" #include <iostream>using namespace std;int _tmain (int argc, _tchar*    Argv[]) {ccsvoperator csvoperator; Csvoperator.loadcsv ("Drawing data. csv");cout<< "line:" <<CSVOperator.indexOfLines<<endl;cout<< "    Column: "<<CSVOperator.indexOfColumn<<endl;    std::string* pstring = csvoperator.getstring (1,600);    if (pstring) {std::cout<< pstring->c_str () << ' \ n ';    } pstring = csvoperator.getstring (2,4);    if (pstring) {std::cout<< pstring->c_str () << ' \ n '; }//std::string* pstring = null;int j = 0;for (int i = 0,ncolconut = Csvoperator.indexofcolumn;i < Ncolconut; ++i) {if (P String = csvoperator.getstring (1,i+1)) {//m_listctrl.  InsertColumn (J, Pstring->c_str (), Lvcfmt_center, 50);    Add 1th column,//cout<< "\ t" <<&pString;cout<< "\ T";p rintf (Pstring->c_str ()); ++j;}} int _int = 0;//if (csvoperatOr.     GetInt (3,1,_int)//{//std::cout<< _int << ' \ n ';//}//float _float = 0.0f;     if (Csvoperator.getfloat (4,1, _float)) {std::cout<< _float<< ' \ n '; } system ("pause"); return 0;}


The effect is as follows:

A C + + class that operates in CVS format

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.