读取文件内容时出现问题
Issue with reading file content
我有一个包含文本的文件。我逐行读取整个文件并附加到字符串对象。但是当我得到最终的字符串打印出来时,我没有得到整个文件内容。我确信这是由于存在特殊字符,例如""、"\r"、"\t"等。
这是我的示例代码:
// Read lines until end of file (null) is reached
do
{
line = "";
inputStream->read_line(line);
cout<<"n "<<line;//here i get the content of each line
fileContent.append(line);// here i am appending
}while(line.compare("") != 0);
这是在
C++中将文件读入内存的方法:
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
using namespace std;
int main() {
vector <string> lines;
ifstream ifs( "myfile.txt" );
string line;
while( getline( ifs, line ) ) {
lines.push_back( line );
}
// do something with lines
}
你必须显示更多的代码,让我知道你的问题是什么。
如果你将整个文件读入一个字符串,这是我通常使用的方法:
#include <string>
#include <fstream>
#include <iterator>
std::string read_file(const char *file_name)
{
std::filebuf fb;
if(!fb.open(file_name, std::ios_base::in))
{
// error.
}
return std::string(
std::istreambuf_iterator<char>(&fb),
std::istreambuf_iterator<char>());
}
相关文章:
- 使用mongocxx驱动程序时包含头文件问题
- 在WSL:configure_file上对config_file的每次调用都失败:配置文件时出现问题
- 编译要在英特尔Hyperscan中使用的.cc文件时出现问题
- 使用 seekg() 读取C++中的文件时出现问题
- sqlite3 和生成文件中的链接出现问题
- C++头文件和类实现出现问题
- 用户输入字符串的文件附加问题..C++
- node-gyp 的先有鸡还是先有蛋的问题:指向依赖项中的头文件
- 读取文件并将其存储在unordered_map中时出现问题
- 编译 llvm 插件时出现问题:llvm/Config/llvm-config.h:没有这样的文件或目录
- 我在使用C++文件时遇到了一些问题
- 在Linux上使用Clang / OLLVM交叉编译helloworld Windows可执行文件时的问题
- 将文件复制到自定义位置,存在字符串转换问题
- DirectX12 的问题:"d3dApp.h":没有这样的文件或目录
- (ODR 使用问题)在不同文件中priority_queue名称相同的结构
- C++模板编程设计问题 - 根据输入文件返回不同的类型
- 读取制表符分隔的文件时出现问题 C++
- 当我在 CLion 中读取数组中的 txt 文件时C++编码问题
- 使用 RapidXML 解析大型 XML(大小大于 65 KB)文件时出现问题
- C++:读取.BMP文件时出现问题;文件结束时间早于预期