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;

}

pasting

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

时间: 2024-10-05 21:51:41

c++ stream操作杂记的相关文章

c#常见stream操作

常见并常用的stream一共有 文件流(FileStream), 内存流(MemoryStream), 压缩流(GZipStream), 加密流(CrypToStream), 网络流(NetworkStream): 1.文件流(读取文件流-输出文件流)FileStream using(Streamstreamwrite=new FileStream(@"D:\BaiduYunDownload\45.avi",FileMode.OpenOrCreate)) { using (Stream

Stream 操作 合并两个list

addAll  添加另一集合里面的元素 add 添加整个集合包括 [] Stream 操作 合并两个lis  出自http://www.it1352.com/963663.html 1 public class Test { 2 public static void main(String[] args) { 3 4 List<String> destList = Collections.synchronizedList(new ArrayList<>(Arrays.asList(

stream操作 z

常见并常用的stream一共有 文件流(FileStream), 内存流(MemoryStream), 压缩流(GZipStream), 加密流(CrypToStream), 网络流(NetworkStream): 1.文件流(读取文件流-输出文件流)FileStream using(Streamstreamwrite=new FileStream(@"D:\BaiduYunDownload\45.avi",FileMode.OpenOrCreate)) { using (Stream

java stream操作案例

1.从List<Staff>筛选符合条件的List<String>: List<Staff> staffListtemp=Lists.newArrayList(); List<String> staffIdList=staffListtemp.stream().filter(staff->deptIdSet.contains(staff.getDeptId())).map(staffId->staffId.getStaffId()).collec

postgres 常规操作杂记

分布式:1.扩容不方便(数据重分布)2.分布键变更很麻烦3.分布键选择(架构设计)谨慎4.跨库join性能差5.分布式事务性能差6.sql限制多,功能确实多7.应用改造成本巨大8.全局一致性时间点恢复几乎不可能实现 一.PGSQL 常规操作citus 分库分表:https://yq.aliyun.com/articles/647368?spm=a2c4e.11153940.0.0.428c3fb76WPkVXhttp://mysql.taobao.org/monthly/2018/01/08/备

mysql 操作杂记

SHOW VARIABLES LIKE 'character%'; SET character_set_client = utf8; SET character_set_connection = utf8; SET character_set_database = utf8; SET character_set_results = utf8; SET character_set_server = utf8; show create database test; show create table

&lt;JAVA8新增内容&gt;关于集合的操作(Collection/Iterator/Stream)

因为下文频繁使用lambda表达式,关于Java中的lambda表达式内容请见: http://www.cnblogs.com/guguli/p/4394676.html 一.使用增强的Iterator遍历集合元素 Iterator接口也是Java集合框架的成员,但它与Collection系列,Map系列的集合不一样:Collection系列集合,Map系列集合主要用于承装其他对象,而Iterator则主要用于遍历(即迭代访问)Collection集合中的元素,Iterator对象也被称为迭代器

详解Java 8中Stream类型的“懒”操作

在进入正题之前,我们需要先引入Java 8中Stream类型的两个很重要的操作: 中间和终结操作(Intermediate and Terminal Operation) Stream类型有两种类型的方法: 中间操作(Intermediate Operation) 终结操作(Terminal Operation) 官方文档给出的描述为[不想看字母的请直接跳过]: Stream operations are divided into intermediate and terminal operat

还看不懂同事的代码?超强的 Stream 流操作姿势还不学习一下

Java 8 新特性系列文章索引. Jdk14都要出了,还不能使用 Optional优雅的处理空指针? Jdk14 都要出了,Jdk8 的时间处理姿势还不了解一下? 还看不懂同事的代码?Lambda 表达式.函数接口了解一下 前言 我们都知道 Lambda 和 Stream 是 Java 8 的两大亮点功能,在前面的文章里已经介绍过 Lambda 相关知识,这次介绍下 Java 8 的 Stream 流操作.它完全不同于 java.io 包的 Input/Output Stream ,也不是大数