C++:读写二进制文件到double数组
生活随笔
收集整理的這篇文章主要介紹了
C++:读写二进制文件到double数组
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在以下的代碼中,我們將寫(xiě)入一個(gè)double數(shù)組到1.txt中,并且讀取出來(lái)。
主要采用了fstream這個(gè)庫(kù),代碼如下:
link
#include <math.h> #include <fstream> #include <iostream> int main(){const int length = 100;double f1[length] ;for (int i = 0; i < length; i++){f1[i] = i + i / 1000.0;}std::ofstream ofs("1.txt", std::ios::binary | std::ios::out);ofs.write((const char*)f1, sizeof(double) * length);ofs.close();double* f2 = new double[length];std::ifstream ifs("1.txt", std::ios::binary | std::ios::in);ifs.read((char*)f2, sizeof(double) * length);ifs.close();for (int i = 0; i < length; i++){std::cout<<f2[i]<<std::endl;}return 0; }總結(jié)
以上是生活随笔為你收集整理的C++:读写二进制文件到double数组的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: C++:读写二进制文件到double数组
- 下一篇: 经济学人网站