【发布时间】:2020-10-19 14:41:45
【问题描述】:
我正在处理一个模拟文件。该文件有许多行,包含数字、字母和字符。
例子:
GR123,7894.5444,A,4687.5643,P
GR456,1234.6556,A,9657.5686,P
GR789,2344.3422,A,9786.8465,P
GR987,6522.6354,A,3245.5754,P
我需要取 A 和 P 之前的值(对于第一行,7894.5444 和 4687.5643)。
如何通过将此字符串定位到 int 进行解析?已经尝试过:
double exampleA = stoi(row.substr(6,9));
double exampleP = stoi(row.substr(18,9));
但它给了我这个错误:request for member ‘substr’ in ‘row’, which is of non-class type ‘char*’
也试过了:
char exampleA[9];
char exampleP[9];
memcpy(&exampleA, &row[6],sizeof(exampleA));
memcpy(&exampleP, &row[6],sizeof(exampleP));
为了在将值从行中分离出来后进行转换,但 memcpy 缓冲区总是将 exampleP 的值与 exampleA 一起带来:
A : 7894.5444
P : 4687.5643?7894.5444
【问题讨论】:
-
您正在尝试对
char*使用std::string语法,为什么不首先使用std::string? -
它不是 CSV 文件,而是 TXT 文件。我会尝试调整这个问题的答案