当给定字符串时,Cout在行首开始打印 我有一个C++中的字符串向量,名为“代码>行< /代码> .
这条线当给定字符串时,Cout在行首开始打印 我有一个C++中的字符串向量,名为“代码>行< /代码> .,c++,string,std,cout,flush,C++,String,Std,Cout,Flush,这条线 std::cout << "First line: >" << lines[0] << "<" << std::endl; std::cout如果没有行[0]的全部内容,就不可能确定,但我(受过教育的)猜测是行[0]以\r结尾,即回车符,因此行[0]之后打印的所有内容都在行的开头打印。行[0]中有什么内容?你能在其他地方复制它吗?这似乎是系统特有的。这是另一件奇怪的事情,在[0]行中有一个正好31个字符的字符串,但当我打印它
std::cout << "First line: >" << lines[0] << "<" << std::endl;
std::cout如果没有行[0]
的全部内容,就不可能确定,但我(受过教育的)猜测是行[0]
以\r
结尾,即回车符,因此行[0]
之后打印的所有内容都在行的开头打印。行[0]中有什么内容?你能在其他地方复制它吗?这似乎是系统特有的。这是另一件奇怪的事情,在[0]行中有一个正好31个字符的字符串,但当我打印它的长度时,它显示为32。我检查过了,最后一个不是endline,只是null。我在ubuntu中尝试过,我可以在大约30分钟内尝试ubuntu gnome,请提供一个。仅从这里提供的代码来看,它没有理由生成您报告的输出是的,这就是问题所在。我试图从一个包含多行的字符串中一行一行地获取这些行,并在“\n”之后进行分析,而分隔符实际上是“\r\n”。我将提供完整的代码来显示问题entirely@VladIordache即使有\r
或其他东西,我认为输出中也不能有两个
#include <iostream>
#include <vector>
#include <string.h>
std::vector<std::string> parse(char *buffer, const char *delimiter) {
std::string buff(buffer);
size_t pos = 0;
std::string part;
std::vector<std::string> parts;
while ((pos = buff.find(delimiter)) != std::string::npos) {
part = buff.substr(0, pos);
parts.push_back(part);
buff.erase(0, pos + 1);
}
parts.push_back(buff);
return parts;
}
int main() {
char *s;
s = strdup("Many lines\r\nOf text");
std::cout << s << std::endl; // this should print the string how it is
std::vector<std::string> lines;
lines = parse(s, "\n"); // parsing the string, after "\n" delimiter
// that was the problem, I should have parsed after "\r\n"
std::cout << "First line: >"<< lines[0] << "<" << std::endl; // output
}