码迷,mamicode.com
首页 > 编程语言 > 详细

c++ stream操作杂记

时间:2018-10-14 16:31:26      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:turn   ali   time_t   注意   lc_all   quick   tin   var   mon   

包含简单的读写文件,供初学者入门,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;
}
pasting

 

c++ stream操作杂记

标签:turn   ali   time_t   注意   lc_all   quick   tin   var   mon   

原文地址:https://www.cnblogs.com/aboycando/p/9785985.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!