這很好用,輸出1 3
std::string s("driver at 1 3");
int c, d;
sscanf(
s.c_str(),
"%*s %*s %d %d",
&c, &d );
std::cout << c <<" "<< d <<"\n";
但這失敗了,輸出6.95129e-310 6.95129e-310
std::string s("driver at 1 3");
double c, d;
sscanf(
s.c_str(),
"%*s %*s %f %f",
&c, &d );
std::cout << c <<" "<< d <<"\n";
我嘗試將輸入更改為,std::string s("driver at 1.0 3.0");但它以完全相同的方式失敗
c 17 windows mingw g v11.2
uj5u.com熱心網友回復:
您的格式字串中有錯誤。您應該增加編譯器輸出的警告:https ://godbolt.org/z/Pfd414o45
#include <string>
#include <cstdio>
#include <iostream>
int main() {
std::string s("driver at 1 3");
double c, d;
sscanf(
s.c_str(),
"%*s %*s %f %f",
&c, &d );
std::cout << c <<" "<< d <<"\n";
}
<source>: In function 'int main()':
<source>:10:19: warning: format '%f' expects argument of type 'float*', but argument 3 has type 'double*' [-Wformat=]
10 | "%*s %*s %f %f",
| ~^
| |
| float*
| %lf
11 | &c, &d );
| ~~
| |
| double*
<source>:10:22: warning: format '%f' expects argument of type 'float*', but argument 4 has type 'double*' [-Wformat=]
10 | "%*s %*s %f %f",
| ~^
| |
| float*
| %lf
11 | &c, &d );
| ~~
| |
| double*
Compiler returned: 0
你需要使用%lf雙倍。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/477974.html
標籤:C
上一篇:g 不能用-I改變包含路徑
下一篇:逐步擦除串列元素
