p11_10.cpp

来自「相当丰富的C++源码」· C++ 代码 · 共 32 行

CPP
32
字号
/***************************************************
* 程序名:p11_10.cpp                                *
* 功能: 二进制文件与文本文件比较                   *
*                    By Antony                      *
*                     02-08-2005                    *
****************************************************/
# include <fstream>
# include <iostream>
using namespace std;
void main() {
	char *name[3]={"Antony","John","Tom"};
	float score[3]={85.5, 90, 60};
	fstream txtfile,binfile;    //建立文件流对象
	txtfile.open("d:\\c++book\\record.txt",ios::out|ios::trunc);
    binfile.open("d:\\c++book\\record.dat",ios::binary|ios::out|ios::trunc);
	if(!txtfile) {
		cerr<<" record.txt open or create error!"<<endl;
		exit(1);
	}
	if(!binfile) {
		cerr<<" record.dat open or create error!"<<endl;
		exit(1);
	}
	for(int i=0;i<3;i++) {
		txtfile<<name[i]<<'\t'<<score[i]<<endl;
		binfile.write(name[i],8*sizeof(char));
		binfile.write((char *)&score[i],sizeof(float));
	}
	txtfile.close();
	binfile.close();
}

⌨️ 快捷键说明

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