C++之文件操作
2008-03-23 09:07
197 查看
//通过文件检索数据
#include <iostream.h >
#include <fstream.h>
class Data
{
private:
char name[25];
char age[3];
char address[51];
char phoneNo[15];
public:
void print()
{
cout << endl << "姓名:" << name;
cout << endl << "年龄:" << age;
cout << endl << "地址:" << address;
cout << endl << "电话号码:" << phoneNo;
}
void get()
{
cout << endl <<"姓名:";
cin >> name;
cout << endl <<"年龄:";
cin >> age;
cin.ignore();
cout << endl <<"地址:";
cin.getline(address,51);
cout << endl <<"电话号码:";
cin >> phoneNo;
}
};
int main()
{
Data object;
fstream file;
char reply = 'Y';
file.open("NewFile.dat",ios::out|ios::app);
while(reply=='Y'||reply=='y')
{
cout <<"请输入个人详细信息" << endl;
object.get();
file.write((char *)&object,sizeof(object));
cout <<"要继续吗?[Y/N]";
cin >> reply;
}
file.close();
file.open("NewFile.dat",ios::in);
file.read((char*)&object,sizeof(object));
while(!file.eof())
{
object.print();
file.read((char*)&object,sizeof(object));
}
file.close();
return 0;
}
#include <iostream.h >
#include <fstream.h>
class Data
{
private:
char name[25];
char age[3];
char address[51];
char phoneNo[15];
public:
void print()
{
cout << endl << "姓名:" << name;
cout << endl << "年龄:" << age;
cout << endl << "地址:" << address;
cout << endl << "电话号码:" << phoneNo;
}
void get()
{
cout << endl <<"姓名:";
cin >> name;
cout << endl <<"年龄:";
cin >> age;
cin.ignore();
cout << endl <<"地址:";
cin.getline(address,51);
cout << endl <<"电话号码:";
cin >> phoneNo;
}
};
int main()
{
Data object;
fstream file;
char reply = 'Y';
file.open("NewFile.dat",ios::out|ios::app);
while(reply=='Y'||reply=='y')
{
cout <<"请输入个人详细信息" << endl;
object.get();
file.write((char *)&object,sizeof(object));
cout <<"要继续吗?[Y/N]";
cin >> reply;
}
file.close();
file.open("NewFile.dat",ios::in);
file.read((char*)&object,sizeof(object));
while(!file.eof())
{
object.print();
file.read((char*)&object,sizeof(object));
}
file.close();
return 0;
}
相关文章推荐
- C++简易文件操作源代码
- C++文件读写操作(一)将字母表写入TXT文本文件
- c++文件读写基本操作
- (补课)borland c++ bulder的文件操作总结-1
- C++中对文件进行读写操作
- linux -> c/c++ 之文件的基本操作
- C++文件读写操作(一) 逐字符读取文件
- C/C++ INI文件操作
- 用Visual C++操作INI文件
- c/c++有关文件读写的操作
- c++ 对文件的操作
- C++ 目录文件操作(持续更新)
- C++文件与文件夹操作(3)--文件与文件夹复制
- C++使用BOOST操作文件、目录
- C/C++文件操作
- C/C++ 文件读写操作总结
- C++中对文件进行读写操作
- C++中对文件进行读写操作
- C++之文件操作
- C++的File类文件操作