二进制文件和文本文件读写方法

来源:互联网 发布:免费梆定域名 编辑:程序博客网 时间:2024/06/10 13:45
#include <iostream>#include <stdlib.h>#include <fstream>#include <string.h>using namespace std;#define SIZE 256#define num 20struct people{char name[num];double weight;int tall;int age;char sex;};int main(){//二进制文件和文本文件读写方法people pe={"杨黔", 50.5, 165, 22, 'f'};//ofstream fout("D:\\people.dat", ios::out | ios::app);//fout << pe.name << " " << pe.age << " " << pe.sex << " "//<< pe.tall << " " << pe.weight << " " << "\n";ofstream fout("D:\\people.txt", ios::binary);fout.write((char *)&pe, sizeof(pe));fout.close();people pel={"Tony", 50.5, 165, 22, 'f'};//ifstream fin("D:\\people.dat");ifstream fin("D:\\people.txt", ios::binary);fin.read((char*)&pel, sizeof(pel));cout<<pel.name<<" "<<pel.age<<" "<<pel.sex<<" "<<pel.tall<<" "<<pe.weight<<endl;//char ch[255];//fin.getline(ch, 255-1, 0);//cout << ch;fin.close();return 0;}

原创粉丝点击