저는 C++에서 lines
이라는 문자열 벡터를 가지고 있습니다.Cout은 문자열이 주어질 때 줄의 시작 부분에서 인쇄를 시작합니다.
이 줄
std::cout << "First line: >" << lines[0] << "<" << std::endl;
인쇄 "> IRST 라인 :> string_here"대신 "첫 번째 줄 :> <을 string_here".
문자열 앞의 현재 줄 시작 부분에 cout이 인쇄를 시작하는 이유는 무엇이며 어떻게 해결할 수 있습니까? 나는 또한 모든 면직물을 없애기 위해 노력했지만 결과는 같았다.
이 내 문제, 가 해결되기 전에을 ilustrates 전체 코드입니다 :
#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
}
라인 [0]에 무엇이 있습니까? – sithereal
다른 곳에서도 재생할 수 있습니까? 그것은 시스템 특정 것으로 보인다. – luk32
이 또 다른 이상한 점은 lines [0]에 정확히 31 자의 문자열이 있지만 길이를 인쇄하면 32라고 표시됩니다. 마지막으로 확인한 것은 endline이 아니라 단지 null입니다. –