文本按行读取
问题
有些文本每行结尾是\r\n,有些文本是\r。如果用getline读取,会导致\r的文本全都读成一行,影响后续操作。
因此只能一个字符一个字符判断,无论哪种文本,都是先出现\r,因此当遇到\r的时候,再查看下一个字符,进行后续操作。
void StringToWstring::readLine(std::istream& input, std::vector<std::string>& SpfAllString)
{
std::stringstream ss;
char c;
while (input.get(c))
{
if (c == '\r')
{
// Peek the next character
char nextChar = input.peek();
if (nextChar == '\n')
{
// If the next character is '\n', consume it
input.get();
break; // Line ends with CRLF
}
else
{
// Line ends with CR
break;
}
}
else if (c == '\n')
{
// Line ends with LF
break;
}
ss << c;
}
SpfAllString.push_back(ss.str());
return;
}
ng.push_back(ss.str());
return;
}