gpt4 book ai didi

c++ - 读取包括空字节在内的所有字符

转载 作者:行者123 更新时间:2023-11-30 05:29:04 25 4
gpt4 key购买 nike

我想从它的文件中读出 chrome 的历史记录。我想获取该文件中的所有字符和空字节。我面临的问题是我只得到文件中文本的一部分。我相信它会因空字节或特殊字符而停止。

这是我目前拥有的代码。

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

int main(){

string str;
std::ifstream in("c:/Users/Petrus/Documents/History"); // I have copy my file into my documents to make sure I'm not interfering with Chrome.
std::stringstream buffer;

if (!in.is_open()){
cout << "Failed to open" << endl;
}
else{
cout << "Opened OK" << endl;
}

buffer << in.rdbuf();

std::string contents(buffer.str());

while (getline(buffer, str))
{
cout << str;
}
in.close();

system("PAUSE");

return 0;
}

如果您想查看位于以下位置的 chrome 历史记录文件:

C:\Users\你的名字\AppData\Local\Google\Chrome\User Data\Default -->历史

(PS 你必须包含隐藏文件才能看到 Appdata。)

提前致谢

最佳答案

std::getline() 应该只用于读取纯文本文件。

要读取任意二进制文件,您应该使用 read()。此外,在您的操作系统上,您必须使用 std::ios::binary 标志打开二进制文件。

关于c++ - 读取包括空字节在内的所有字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36547000/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com