C++讀取檔案,將檔案內容讀取到struct中

來源:互聯網
上載者:User

struct定義:

#include "stdafx.h"//記憶體對齊1位元組#pragma pack(1)struct Day{int DateTime;int Open;int High;int Low;int Close;};#pragma pack()

指標讀取:

// Test.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "Day.cpp"#include <sys\stat.h>using namespace std;int _tmain(int argc, _TCHAR* argv[]){fstream f;const char* filename = "e:\\t.dat";f.open(filename,ios::binary|ios::in);struct _stat info;_stat(filename,&info);int filesize = info.st_size;const int SIZE_OF_DAY = sizeof(Day);cout<<"sizeof(Day)="<<SIZE_OF_DAY<<endl;const int days_count = filesize/sizeof(Day);cout<<"day_count="<<days_count<<endl;Day* day = new Day[days_count];//Day* dayTemp=day;for(int i=0;i<days_count;i++){Day* p2Day = day + i;f.read((char*)p2Day,SIZE_OF_DAY);cout<<p2Day->DateTime<<endl;cout<<p2Day->Close<<endl;}f.close();delete[] day;system("pause");return 0;}

  

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.