c++ stream操作杂记
包含简单的读写文件,供初学者入门,stream目前包含三种,终端,文件,内存,注意宽字节。
// "C:\Program Files (x86)\Microsoft Visual C++ Build Tools\vcbuildtools.bat"
// "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.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] = {'\0'};
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;
}
刘总