将文件读取到C ++中的字符串 [英] Reading a file to a string in C++

查看:87
本文介绍了将文件读取到C ++中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一个刚刚接触C ++并来自Python背景的人,我正试图将下面的代码翻译成C ++。

  f = open('transit_test.py')
s = f.read()

什么是最短的C ++习惯做这样的事情?

解决方案

>

  #include< string> 
#include< iterator>
#include< fstream>

使用namespace std;

wifstream f(Ltransit_test.py);
wstring s(istreambuf_iterator< wchar_t>(f),(istreambuf_iterator< wchar_t>()));


As somebody who is new to C++ and coming from a python background, I am trying to translate the code below to C++

f = open('transit_test.py')
s = f.read()

What is the shortest C++ idiom to do something like this?

解决方案

The C++ STL way to do this is this:

#include <string>
#include <iterator>
#include <fstream>

using namespace std;

wifstream f(L"transit_test.py");
wstring s(istreambuf_iterator<wchar_t>(f), (istreambuf_iterator<wchar_t>()) );

这篇关于将文件读取到C ++中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆