gpt4 book ai didi

C++ - 我使用 fin.ignore() 不正确吗?

转载 作者:行者123 更新时间:2023-11-30 03:26:46 24 4
gpt4 key购买 nike

我有一个名为“1.txt”的 .txt 文件,我想读入它。由于该文件以 8 个 BOM 字符开头,如果我执行以下操作:

ifstream fin("1.txt");

string temp = "";

char c = fin.get();

while (!fin.eof())
{
if (c >= ' ' && c <= 'z')
{
temp += c;
}

c = fin.get();
}

cout << temp;

这将不会打印任何内容,因为 BOM 正在执行某些操作。

因此,我决定使用 fin.ignore() 函数,以忽略文件的开头 BOM 字符。但是,仍然没有打印任何内容。这是我的完整程序:

#include <iostream>
#include <fstream>
#include <string>
#include <istream>

using namespace std;

int main()
{
ifstream fin("1.txt");

if (fin.fail())
{
cout << "Fail\n";
}

else
{
string temp = ""; // Will hold 1.txt's contents.

fin.ignore(10, ' ');
// Ignore first 10 chars of the file or stop at the first space char,
// since the BOM at the beginning is causing problems for fin to read the file.
// BOM is 8 chars, I wrote 10 to just be safe.

char c = fin.get();

while (!fin.eof())
{
if (c >= ' ' && c <= 'z') // checks if c stores a standard char.
{
temp += c;
}

c = fin.get();
}

cout << temp;

// PROBLEM: No text is printed to the screen from the above command.

cout << temp.size(); // prints 0
}
}

我假设在:ifstream fin("1.txt"); 之后线,已经太晚了,因为 BOM 可能影响了 fin 的东西。所以我需要以某种方式告诉 fin 在它读入文件之前忽略 BOM 字符,但我不能使用 fin.ignore() 因为我还没有声明 fin 对象。

此外,我知道我可以从我的 .txt 文件中手动删除 BOM,但我正在寻找一种只涉及我编写 C++ 程序的解决方案。如果我有数千或数百万个 .txt 文件,则无法手动删除。另外,我不想下载新软件,例如 Notepad++

这是我在文件“1.txt”中的所有内容:

ÐÏࡱá Hello!

本网站的格式不允许我显示,但在实际文件中,BOM 和 Hello! 之间大约有 15 个空格!

最佳答案

根据 cppreference , 值为\x1a 的字符在 Windows 上以文本模式终止输入。你大概在一开始就有这样的角色。我的空 .doc 文件有一个作为第 7 个字节。

你应该以二进制模式读取文件:

std::ifstream fin("1.txt", std::ios::binary);

您仍然可以使用 ignore忽略前缀。但是,在特定字符之前,它有点片状忽略。二进制前缀可以包含该字符。如果这些前缀的长度始终相同,则忽略特定数量的字节就足够了。此外,您不能依靠在记事本中查看文件来计算字节数。有相当多的隐形字符。您应该查看文件的十六进制 View 。许多优秀的文本编辑器都可以做到这一点,或者您可以使用 Powershell 的 Format-Hex -Path <path>命令。例如,这是我的前几行:

00000000   D0 CF 11 E0 A1 B1 1A E1 00 00 00 00 00 00 00 00  ÐÏ.ࡱ.á........
00000010 00 00 00 00 00 00 00 00 3E 00 03 00 FE FF 09 00 ........>...þ...
00000020 06 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 ................

在没有更多信息的情况下,不清楚删除前缀的最佳方法是什么。

关于C++ - 我使用 fin.ignore() 不正确吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48048147/

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