⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 binaryfiles.cpp

📁 C++&datastructure书籍源码,以前外教提供现在与大家共享
💻 CPP
字号:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
#include "prompt.h"
#include "date.h"

// illustrates reading/writing raw bits, binary files

int main()
{
    string filename = PromptString("file for storing Dates: ");
    int limit =       PromptRange("# of Dates ",10,10000);
	Date today;
    int start = today.Absolute();
    string text = filename + ".txt";
    string binary = filename + ".bin";
	cout << "testing program on " << today << endl;
    
    ofstream toutput(text.c_str());     // open text file
    int k;
    for(k=start; k < start+limit; k++)  // write text form of dates
    {   toutput << Date(k) << endl;
    }
    toutput.close();
    
	// open binary file, write raw dates
    ofstream boutput(binary.c_str(),ios_base::binary);
    for(k=start; k < start+limit; k++)
    {   Date d(k);
        boutput.write(reinterpret_cast<const char *>(&d),sizeof(d));
    }
    boutput.close();
    
	// open input file to read raw dates from
    ifstream input(binary.c_str(),ios_base::binary);
    input.seekg(0,ios_base::beg);     // to the beginning
    streampos startp= input.tellg();  // position of start
    input.seekg(0,ios_base::end);     // seek to end of stream
    streampos endp = input.tellg();   // position of end
    int size = endp-startp;           // number of entries

    cout << "size of file: "  << size << ", # dates = "
		 << size/sizeof(Date) << endl;
    
	// read alldates in file, start at front
    input.seekg(0, ios_base::beg);
    for(k=0; k < size/sizeof(Date); k++)
    {   input.read(reinterpret_cast<char *>(&today),sizeof(today));
		cout << today << endl;
    } 
    return 0;
}
    
    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -