3 回答

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超4個(gè)贊
double占8字節(jié)。
1.用二進(jìn)制模式打開(kāi)文件
FILE* fd = fopen("your_file_path","rb");
2.精確定位文件到你要的數(shù)據(jù)處,(比如用fseek())
3。按字節(jié)讀取8字節(jié)
double indata;
fread(&indata,1,8,fd);
4.如有必要(搞清你的二進(jìn)制文件與本機(jī)的字節(jié)序是否相同),調(diào)整字節(jié)序
char* p = &indata;
char t = p[0]; p[0]=p[7]; p[7]=t; t=p[1]; p[1]=p[6]; p[6]=t; .....
5.使用得到的double值
上例中的indata就是讀到的值

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超6個(gè)贊
方法1,最簡(jiǎn)單的,cin
1 2 3 4 5 6 7 8 9 | #include <iostream> using namespace std; int main() { double dfloat; cin>>dfloat; cout<<"input: "<<dfloat; return 0; } |
方法2,scanf
1 2 3 4 5 6 7 8 9 10 | #include <iostream> #include <cstdio> using namespace std; int main() { double dfloat; scanf("%lf", &dfloat); //注意是lf,對(duì)應(yīng)double cout<<"input: "<<dfloat; return 0; } |
- 3 回答
- 0 關(guān)注
- 1214 瀏覽
添加回答
舉報(bào)