包含简单的读写文件,供初学者入门,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] = {'