• c++ stream操作杂记


    包含简单的读写文件,供初学者入门,stream目前包含三种,终端,文件,内存,注意宽字节。

    // "C:Program Files (x86)Microsoft Visual C++ Build Toolsvcbuildtools.bat"
    // "C:Program Files (x86)Microsoft Visual Studio 14.0VCvcvarsall.bat"
    // g++ -O2 -std=c++11 lock.cpp
    // cl.exe /O2 /std:c++11 lock.cpp
    // cl.exe /O2 /std:c++14 stream.cpp

    #include <iostream> // iostream istream ostream | wxxx
    #include <fstream> // fstream ifstream ofstream | wxxx
    #include <sstream> // stringstream istringstream ostringstream | wxxx
    #include <string>
    #include <ctime>

    // #include <cstddef>
    // #include <cstring>

    using namespace std;

    // wstring => string
    std::string wstring2string(const std::wstring &ws)
    {
    std::string strLocale = setlocale(LC_ALL, "");
    const wchar_t *wchSrc = ws.c_str();
    int nDestSize = wcstombs(NULL, wchSrc, 0) + 1;
    char *chDest = new char[nDestSize](); // vs init zero
    wcstombs(chDest, wchSrc, nDestSize);
    std::string strResult = chDest;
    delete[] chDest;
    setlocale(LC_ALL, strLocale.c_str());
    return strResult;
    }
    // string => wstring
    std::wstring string2wstring(const std::string &s)
    {
    std::string strLocale = setlocale(LC_ALL, "");
    const char *chSrc = s.c_str();
    int nDestSize = mbstowcs(NULL, chSrc, 0) + 1;
    wchar_t *wchDest = new wchar_t[nDestSize]();
    mbstowcs(wchDest, chSrc, nDestSize);
    std::wstring wstrResult = wchDest;
    delete[] wchDest;
    setlocale(LC_ALL, strLocale.c_str());
    return wstrResult;
    }

    bool logFile(string fileName, string fileText, int mode)
    {
    //fstream oftextStream(fileName.c_str(), mode); // ofstream::out ofstream::app
    fstream oftextStream(fileName.c_str(), (ios_base::openmode)mode);
    if (oftextStream)
    {
    char mbstr[64] = {''};
    time_t timer = time(NULL); // ctime(&timer);// unsafe and endeith newline
    std::strftime(mbstr, sizeof(mbstr), "%a %F %T", std::localtime(&timer));
    oftextStream << mbstr << " " << fileText.c_str() << endl;
    oftextStream.close();
    }
    return true;
    }

    inline std::ostream &putline(std::ostream &os, std::string const &line)
    {
    return os << line << std::endl;
    }

    bool dealFile(string fileName, string fileText)
    {
    ostringstream stmtext;
    stmtext << ";" << endl;
    string strstext = stmtext.str();
    const char *cstrs = strstext.c_str();

    fstream iftextStream(fileName.c_str(), ofstream::in);
    if (iftextStream)
    {
    //iftextStream.read(chartext, sizeof(chartext)); char chartext[64] = { 0 };
    // iftextStream >> strstext; // only read end if space
    getline(iftextStream, strstext);
    iftextStream.close();
    }
    //fstream with args
    fstream oftextStream(fileName.c_str(), ofstream::out);
    if (iftextStream)
    {
    //iftextStream.write(chartext, sizeof(chartext)); char chartext[64] = { 0 };
    oftextStream << fileText << endl; // or putline(oftextStream, fileText);
    oftextStream.flush(); // please quickly flush to file
    oftextStream.close();
    }

    //ifstream ofstream with args
    string lineText = "lineText";
    ifstream itextStream(fileName.c_str());
    if (!itextStream)
    {
    return false;
    }
    while (getline(itextStream, strstext))
    { //! textAStream.eof();textAStream.read(charStrs, charSize);
    string strNum = (strstext);
    }
    itextStream.close();
    //ifstream ofstream with args
    ofstream otextStream(fileName.c_str());
    if (!otextStream)
    {
    return false;
    }
    putline(otextStream, lineText); // or otextStream << fileText << endl;
    otextStream.flush(); //please quickly flush to file
    otextStream.close();

    return true;
    }

    int main(int argc, char *argv[])
    {
    dealFile("test.txt", "Hello world");
    logFile("test.txt", "Hello world", fstream::out);
    logFile("test.txt", "Hello world", fstream::app);
    logFile("test.txt", "Hello world", fstream::app);
    return 0;
    }
    pasting
    刘总
  • 相关阅读:
    《英文论文写作再也不难了(工具篇),不收藏就找不到了》
    teachable-machine:探索机器学习如何工作,浏览器中实时浏览
    rasa_core:基于机器学习的对话引擎
    DeepMoji:机器学习模型分析情绪, 情感
    TensorFlow LSTM 注意力机制图解
    Fabrik – 在浏览器中协作构建,可视化,设计神经网络
    移动深度学习 Mobile-deep-learning(MDL)
    Serpent.AI
    face-alignment:用 pytorch 实现的 2D 和 3D 人脸对齐库
    ZhuSuan 是建立在Tensorflow上的贝叶斯深层学习的 python 库
  • 原文地址:https://www.cnblogs.com/aboycando/p/9785985.html
Copyright © 2020-2023  润新知