简体   繁体   English

为什么我在我的文件数据之前收到这些无效字符?

[英]Why am i getting these invalid characters before my file data?

在此处输入图片说明

I am trying to read a file into a string either by getline function or fileContents.assign( (istreambuf_iterator<char>(myFile)), (istreambuf_iterator<char>()));我试图通过getline函数或fileContents.assign( (istreambuf_iterator<char>(myFile)), (istreambuf_iterator<char>())); Either of the way gives me the above output which shown in the image.无论哪种方式都给了我上面显示的输出图像。

First way:第一种方式:

 string fileContents;
 ifstream myFile("textFile.txt");
 while(getline(myFile,fileContents))
 cout<<fileContents<<endl;

Alternate way:替代方式:

 string fileContents;
 ifstream myFile(fileName.c_str());
 if (myFile.is_open())
  {
    fileContents.assign( (istreambuf_iterator<char>(myFile) ),
                       (istreambuf_iterator<char>()    ) );
    cout<<fileContents;
  }

The file begins with those characters, most likely a BOM to tell you what the encoding of the file is.该文件以这些字符开头,很可能是一个BOM,用于告诉您文件的编码是什么。

You probably are not able to see them in Windows Notepad because Notepad hides the encoding bytes.您可能无法在 Windows 记事本中看到它们,因为记事本隐藏了编码字节。 Get a decent text editor that lets you see the binary of the file and you will see those characters.获得一个不错的文本编辑器,它可以让您查看文件的二进制文件,并且您将看到这些字符。

Your file starts with a UTF-8 BOM (bytes 0xEF 0xBB 0xBF ).您的文件以 UTF-8 BOM 0xEF 0xBB 0xBF (字节0xEF 0xBB 0xBF )。 You are reading the file's raw bytes as-is and outputting them to a display that is using an OEM font for codepage 437 .您正在按原样读取文件的原始字节,并将它们输出到使用代码页 437的 OEM 字体的显示器。 To handle text files properly, especially Unicode-encoded text files, you need to read the first few bytes, check for a BOM (and there are several you can look for ), and if detected then seek past the BOM and interpret the remaining bytes of the file in the specified encoding, in this case UTF-8.要正确处理文本文件,尤其是 Unicode 编码的文本文件,您需要读取前几个字节,检查 BOM( 您可以查找几个),如果检测到,则寻找 BOM 并解释剩余的字节指定编码的文件,在本例中为 UTF-8。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM