相关文章推荐
打盹的板栗  ·  前端传递JSON数组数据到后端(解析方法)_ ...·  1 周前    · 
冷冷的草稿本  ·  txt文件转数组_python读取txt为数组·  1 周前    · 
淡定的盒饭  ·  ASP.NET 核心 Blazor ...·  1 周前    · 
傻傻的馒头  ·  STRING_SPLIT ...·  1 周前    · 
坚韧的啤酒  ·  表达式和函数 - Azure Data ...·  5 天前    · 
有胆有识的酸菜鱼  ·  使用vscode时,git所说的“无法将对象 ...·  1 年前    · 
重感情的大熊猫  ·  c语言中指针与数组的异同分析_数组和指针作为 ...·  2 年前    · 
稳重的八宝粥  ·  What does ...·  2 年前    · 
Code  ›  C++:将文件内容读入wstring开发者社区
c++ string
https://cloud.tencent.com/developer/ask/sof/112616044
没人理的大熊猫
12 月前
首页
学习
活动
专区
工具
TVP 最新优惠活动
返回腾讯云官网
提问

问 C++:将文件内容读入wstring

Stack Overflow用户
提问于 2015-11-05 09:20:38
EN

在C++中读取文件内容时,我面临一个问题。

我有一个包含内容"Password=(|OBFUSCATED:A;Q=K?LNQGMXNLd282>;3Cl*I(n7$:OBFUSCATED|)".的文本文件

当我试图读取该文件并将其内容保存到wstring时,不读取完整的文件内容,而是只将“Password=(困惑:”)读入wstring变量中。

Codesnippet是:

代码语言: javascript
复制
std::wifstream input1(path);
std::wstring content((std::istreambuf_iterator<wchar_t>(input1)),
        (std::istreambuf_iterator<wchar_t>()));

在读取文件内容时需要帮助。

提前谢谢!!

拉萨

2 1.4K 0 票数 2
EN
c++

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-11-05 09:30:13

在一个分支上,将 binary 添加到打开的标志中:

代码语言: javascript
复制
std::wifstream input1(path, std::ios::binary);
std::wstring content((std::istreambuf_iterator<wchar_t>(input1)),
    {});
票数 3
EN

Stack Overflow用户

发布于 2022-04-10 02:26:24

从几个地方收集信息..。这应该是最快和最好的方法:

代码语言: javascript
复制
#include <filesystem>
#include <fstream>
#include <string>
//Returns true if successful.
bool readInFile(std::string pathString)
  //Make sure the file exists and is an actual file.
  if (!std::filesystem::is_regular_file(pathString))
    return false;
  //Convert relative path to absolute path.
  pathString = std::filesystem::weakly_canonical(pathString);
  //Open the file for reading (binary is fastest).
  std::wifstream in(pathString, std::ios::binary);
  //Make sure the file opened.
  if (!in)
    return false;
  //Wide string to store the file's contents.
  std::wstring fileContents;
  //Jump to the end of the file to determine the file size.
  in.seekg(0, std::ios::end);
  //Resize the wide string to be able to fit the entire file (Note: Do not use reserve()!).
  fileContents.resize(in.tellg());
  //Go back to the beginning of the file to start reading.
  in.seekg(0, std::ios::beg);
  //Read the entire file's contents into the wide string.
  in.read(fileContents.data(), fileContents.size());
 
推荐文章
打盹的板栗  ·  前端传递JSON数组数据到后端(解析方法)_element-china-area-data 前端json数组怎么转换成字符串传到后端
1 周前
冷冷的草稿本  ·  txt文件转数组_python读取txt为数组
1 周前
淡定的盒饭  ·  ASP.NET 核心 Blazor 渲染性能最佳做法 | Microsoft Learn
1 周前
傻傻的馒头  ·  STRING_SPLIT (Transact-SQL) - SQL Server | Microsoft Learn
1 周前
坚韧的啤酒  ·  表达式和函数 - Azure Data Factory & Azure Synapse | Microsoft Learn
5 天前
有胆有识的酸菜鱼  ·  使用vscode时,git所说的“无法将对象迁移到永久存储空间”是什么意思?-腾讯云开发者社区-腾讯云
1 年前
重感情的大熊猫  ·  c语言中指针与数组的异同分析_数组和指针作为函数参数的异同_guotianqing的博客-CSDN博客
2 年前
稳重的八宝粥  ·  What does getResources().getIdentifier() do in android? - Stack Overflow
2 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号