0

我有一个打包大量文件的二进制文件(类似于 .tar),我可以在其中找到二进制文件和文本文件。

在内存字符串中处理时,回车行通常是'\n',但如果我从这个打包文件中读取文本部分,我会得到"\r\n"。因此,处理此文本会给我带来错误。

下面是从二进制文件中读取文本的代码:

FILE* _fileDescriptor;                        // it's always open to improve performance
fopen_s(&_fileDescriptor, _filePath.string().c_str(), "rb"); 

char* data = new char[size + 1];              // size is a known and correct value
fseek(_fileDescriptor, begin, SEEK_SET);      // begin is another known value, where the file starts inside the packed one
fread(data, sizeof(char), size, _fileDescriptor);
data[it->second.size] = '\0';

这给了我正确的文本到data,但是下面的代码在读取空行时给了我错误:

istringstream ss(data);      // create a stringstream to process it in another function
delete[] data;               // free the data buffer

// start processing the file
string line;
getline(infile, line);       // read an empty line

if(line.size() > 0) {
    /*
     enters here, because the "empty" line was "\r\n", and now the value of line is '\r', therefore line.size() == 1
    */
    ...

那么,有什么建议可以避免使用“\r”吗?

我在记事本++上编辑了它。将其配置更改为使用 '\n' 而不是 '\r\n' 作为换行符,但我不想依赖这个,因为其他人可能会错过它,如果那个会发生。

4

1 回答 1

1

可能最容易从字符串中修剪 '\r' 字符,然后丢弃空行。有关修剪 std::string 的方法,请参阅此答案(我假设这就是“行”):

修剪 std::string 的最佳方法是什么?

于 2012-12-19T00:23:56.013 回答