我已经在GoogleTest中编写了以下测试。要做的所有事情就是读取一个csv文件并将其打印到控制台,用分号替换逗号。
TEST(FlaAlgoGyroAnomalyBehavioral, imu_2_gz_stiction){
std::ifstream input_file("../imu_2_gz_anom_test.csv");
std::string line;
int ctr = 0;
std::stringstream output;
while(std::getline(input_file, line)){
ctr++;
if (ctr <= 3){
continue;
}
if (ctr >= 10){
break;
}
std::vector<std::string> vect;
std::istringstream ss(line);
std::string token;
while (std::getline(ss, token, ',')){
token.append(";");
output << token;
}
output << std::endl;
}
output.flush();
FAIL() << output.str();
}
由于某种原因,它仅显示以下内容:
;
;
;
;
;
;
但是,当我删除token.append(";");
行时,我得到了想要的输出(当然没有分号):
2970119168634936-0.012451171875-0.0080566406251.011474609375-0.3967285156250.5645751953125-1.0986328125-0.01220703125-0.0114746093750.9990234375-1.2359619140625-0.8087158203125-0.1068115234375-0.00927734375-0.0129394531251.0068359375-1.037597656250.30517578125-0.671386718750.008056640625-0.01171875-0.99462890625-0.518798828125-0.213623046875-0.183105468750.006591796875-0.009033203125-0.984619140625-0.2136230468750.6103515625-0.59509277343750.009765625-0.0078125-0.98339843750.0457763671875-0.74768066406250.045776367187536.3880126182965335.5914826498422736.9400630914826535.3627760252365934.3927444794952735.28391167192429-2.6528878649401122e-081.654520929150749e-070.0002421037497697398-0.0086239615484373640.0038406856219808105-0.00744516910344827925.2812289589963121
2970219168714936-0.01220703125-0.0080566406251.01220703125-0.427246093750.5645751953125-1.0986328125-0.01171875-0.011718750.99853515625-1.40380859375-1.11389160156250.0-0.009033203125-0.013183593751.00732421875-0.99182128906250.2288818359375-0.915527343750.0078125-0.01171875-0.99462890625-0.4119873046875-0.213623046875-0.183105468750.006591796875-0.009033203125-0.9841308593750.16784667968750.335693359375-0.6408691406250.009765625-0.007568359375-0.9836425781250.2593994140625-0.7781982421875-0.259399414062536.380126182965335.5914826498422736.9321766561514235.3548895899053634.4085173501577335.27602523659306-3.641800461195998e-081.1611859207505403e-070.0002421232493361458-0.0121207276606583040.008199632247851696-0.00632870342087699125.281208200785641
2970319168794936-0.01220703125-0.00781251.011474609375-0.41198730468750.5645751953125-1.0833740234375-0.012451171875-0.011718750.9990234375-1.40380859375-0.8392333984375-0.091552734375-0.009033203125-0.0129394531251.0068359375-0.36621093750.335693359375-0.7629394531250.00830078125-0.011474609375-0.994384765625-0.3814697265625-0.1983642578125-0.19836425781250.00634765625-0.0087890625-0.984619140625-0.2746582031250.6256103515625-0.549316406250.009521484375-0.0078125-0.98339843751.312255859375-0.8087158203125-0.4882812536.3958990536277635.6072555205047436.9479495268138835.3706624605678234.4085173501577335.29968454258675-7.500008614158558e-091.4504682610549935e-070.00024209351977333426-0.0088361466623609890.008187172170437407-0.00522122172696981625.2813222506068881
2970419168874936-0.011962890625-0.00781251.01171875-0.427246093750.5645751953125-1.1444091796875-0.011962890625-0.011718750.9990234375-1.2054443359375-1.03759765625-0.0152587890625-0.00927734375-0.0129394531251.0068359375-0.427246093750.3662109375-0.50354003906250.0078125-0.011962890625-0.9951171875-0.3814697265625-0.1983642578125-0.1525878906250.0068359375-0.0087890625-0.984863281250.2441406250.3814697265625-0.65612792968750.009765625-0.008056640625-0.9841308593751.068115234375-0.6866455078125-0.106811523437536.4116719242902235.599369085173536.9479495268138835.3943217665615234.3927444794952735.25236593059937-3.643257784347043e-081.3461932724112557e-070.0002421726530883461-0.0102891171991359440.00894796357897576-0.00544272234037634925.2812807642699051
2970519168954936-0.012451171875-0.0080566406251.01220703125-0.41198730468750.579833984375-1.1444091796875-0.01171875-0.0114746093750.999267578125-1.64794921875-0.946044921875-0.0457763671875-0.009033203125-0.0129394531251.007568359375-1.05285644531250.2899169921875-0.68664550781250.008056640625-0.01171875-0.994873046875-0.3204345703125-0.1678466796875-0.183105468750.007080078125-0.008544921875-0.98486328125-0.28991699218750.5645751953125-0.53405761718750.009521484375-0.0078125-0.9843750.06103515625-0.7019042968750.09155273437536.3880126182965335.63091482649842636.9637223974763435.3785488958990534.38485804416403435.26813880126183-5.308616213284267e-091.4426949235257783e-070.00024223169020842764-0.0090837602328974750.0067885839598602615-0.00798822657088749125.2813015224805751
2970619169034936-0.01171875-0.00781251.011962890625-0.44250488281250.6103515625-1.1444091796875-0.011962890625-0.011718750.998779296875-1.1138916015625-0.946044921875-0.1068115234375-0.0087890625-0.0129394531251.0068359375-1.037597656250.1983642578125-0.915527343750.008056640625-0.01171875-0.99462890625-0.30517578125-0.152587890625-0.1525878906250.006591796875-0.0087890625-0.9846191406250.2746582031250.42724609375-0.7019042968750.009765625-0.0078125-0.9836425781250.2288818359375-0.8087158203125-0.21362304687536.3958990536277635.62302839116719636.9479495268138835.3706624605678234.400630914826535.26813880126183-1.773620361689154e-081.0525636184866016e-070.00024212291464209557-0.0105748458736343310.007961260052979924-0.00717816010364913325.2812911734596041
我不明白这里发生了什么!
答案 0 :(得分:0)
最可能的解释是您正在读取的CSV文件具有Windows行尾\r\n
。读取行的getline
调用将读取\r
并丢弃换行符,结果每行的最后一个字段将以单独的回车结束。然后添加的分号将放在回车符之后,并在左边缘打印。
显然,当您随后使用std::eol
发送换行符时,终端仿真器将清除输出行的其余部分,当回车符后紧跟换行符时,它不会执行此操作。这似乎是奇怪的行为,但是我敢肯定,实施它的人都有很好的意图。
请尝试删除以dos2unix
结尾的Windows行,或者在创建line
之前检查istringstream
的最后一个字符是否为回车符(如果是,请擦除回车符,如果是,则将其删除)。