c++中的文件操作
file manipulation in c++
我将python中的此代码转换为c++:
content = file(filename, "rb").read()
这是c++中的代码:
ifstream file;
file.open(filename, fstream::binary);
file.seekg (0, ios::end);
long fileLength = file.tellg();
file.seekg(0, ios_base::beg);
char *content = new char[fileLength];
file.read(content, fileLength);
当我运行python代码时,我在内容中得到一个长字符串(500个字符~),而c++代码只返回4个字符。
有什么建议吗?
感谢
读取整个文件的最简单方法是:
std::string content(
std::istreambuf_iterator<char>(std::ifstream(filename, std::fstream::binary).rdbuf()),
std::istreambuf_iterator<char>());
相关文章:
- C++二进制文件 I/O 操作速度变慢.数据库如何处理二进制文件?
- RPN计算器使用头文件进行计算操作和堆栈;用于堆栈的矢量
- 我是否缺少<实验/文件系统>移动操作?
- C++ 使用读取目录更改监视文件更改W不触发所有操作?
- 在链接的程序集文件中,我想从 c++ 调用代码访问变量.是否可以在不触发访问冲突的情况下执行此操作?
- 程序未正确执行文件操作
- 尝试在 QT 中控制文件-QMenu 操作
- 打开一个文件,修改每个字符,然后执行反向操作不输出原始文件
- 如何在我的进程中运行的二进制文件中注册操作和内核
- exe文件不被识别为内部或外部命令,可操作的程序或批处理文件在C++
- C++程序在执行文件操作时在Windows与Linux上运行得慢得多?
- 如何处理对生成文件中依赖项的重命名操作?
- 使用文件操作的 XMLFile 解析
- 从文件中读取运算符并执行操作C++
- 为什么我的文件/目录操作在Windows上工作正常,但在Linux上不能
- 使用函数操作文件
- 试图在c++中操作文件
- 除了 Boost 的文件系统之外,是否有一个好的C++库来操作文件和目录?
- C++:操作文件资源
- Qt Creator在使用其C++编辑器操作文件时崩溃