#include<fstream>
#include<string>
#include<iomanip>
using namespace std
class staff
{
public:
string name//员工姓名
string sex//员工性别
string birthday//出生日期
int age//年龄
string marriage//婚姻状况
string address//家庭住址
string title//职称
int salary//工资
staff()//构造函数
{
name = '\0'
sex = '\0'
birthday = '\0'
age = 0
marriage = '\0'
address = '\0'
title = '\0'//职称
salary = 0
}
void save_new()//保存员工信息到staff.txt
{
ofstream outData("staff.txt", ios::app)
if (!outData)
{
cout<<endl <<"对不起!!!打开文件失败!!!!" <<endl <<endl
system("pause")
return
}
outData << setiosflags(ios::left)<<setw(8)<<name
<<"" << setw(6) << salary
<<setw(3) <<sex <<" "
<< setw(13) << birthday <<" "
<< setw(5) <<age <<" "
<< setw(5) <<marriage <<" "
<< setw(15) <<address <<" "
<< setw(5) <<title <<endl
outData.close()
}
void add_person()//添加员工信息
{
cout <<endl <<"根据下面提示录入职工信息" <<endl <<endl
<<"姓名:"
cin >>name
cout <<"性别:"
cin >>sex
cout <<"出生年月:"
cin >>birthday
cout <<"年龄:"
cin >>age
cout <<"婚姻状况:"
cin >>marriage
cout <<"家庭住址:"
cin >>address
cout <<"职称:"
cin >>title
cout <<"工资:"
cin >>salary
save_new()
cout <<endl <<endl <<"新的职工信息已经保存好!!!!!" <<endl
}
}
int main()
{
staff a,b,c,d,e//定义五个员工对象
//输入员工信息
a.add_person()
b.add_person()
c.add_person()
d.add_person()
e.add_person()
ifstream in("staff.txt")
staff s,k,j
//读取第一个员工的信息
in>>s.name>>s.salary>>s.sex>>s.birthday>>s.age>>s.marriage>>s.address>>s.title
//输出第一个员工的信息
cout<< setiosflags(ios::left)<<setw(8)<<s.name<<"" << setw(6) << s.salary
<<setw(3) <<s.sex <<" "<< setw(13) << s.birthday <<" "<< setw(5) <<s.age <<" "
<< setw(5) <<s.marriage <<" "<< setw(15) <<s.address <<" "<< setw(5) <<s.title<<endl
in.seekg(71,ios::cur)//光标移过员工类,到达第三个员工信息处,71是格式设置里面所有宽度之和
//读取第三个员工的信息
in>>k.name>>k.salary>>k.sex>>k.birthday>>k.age>>k.marriage>>k.address>>k.title
//输出第三个员工的信息
cout<< setiosflags(ios::left)<<setw(8)<<k.name<<"" << setw(6) << k.salary
<<setw(3) <<k.sex <<" "<< setw(13) << k.birthday <<" "<< setw(5) <<k.age <<" "
<< setw(5) <<k.marriage <<" "<< setw(15) <<k.address <<" "<< setw(5) <<k.title<<endl
in.seekg(71,ios::cur)//光标移过员工类,到达第五个员工信息处
//读取第三个员工的信息
in>>j.name>>j.salary>>j.sex>>j.birthday>>j.age>>j.marriage>>j.address>>j.title
//输出第三个员工的信息
cout<< setiosflags(ios::left)<<setw(8)<<j.name<<"" << setw(6) << j.salary
<<setw(3) <<j.sex <<" "<< setw(13) << j.birthday <<" "<< setw(5) <<j.age <<" "
<< setw(5) <<j.marriage <<" "<< setw(15) <<j.address <<" "<< setw(5) <<j.title<<endl
in.close()
}
文件随机读写完整c程序:#include<stdio.h>
#include<stdlib.h>
#define NUM 20
main( )
{
FILE *fp1/*定义文件指针*/
char *temp
int i,j
struct rec{ /*定义结构体类型*/
char id[10]
char name[15]
char department[15]
}record[NUM]
printf("***********************************************************\n")
printf("** This program is to show the random file input &output**\n")
printf("***********************************************************\n")
if ((fp1=fopen("d:\\infile.txt","wb"))==NULL) /*以二进制只写方式打开文件*/
{
printf("cannot open file")/*出错返回*/
exit(1)
}
for( i=0i<NUMi++)
{
printf("Please input id:")
scanf("%s",record[i].id)/*从键盘输入*/
printf("Please input name:")
scanf("%s",record[i].name)
printf("Please input department:")
scanf("%s",record[i].department)
fwrite(&record[i],sizeof(struct rec),1,fp1)/* 成块写入*/
}
fclose(fp1)/*关闭*/
if((fp1=fopen("d:\\infile.txt","rb+"))==NULL)/*以可读写方式打开文件*/
{
printf("cannot open file")/*出错返回*/
exit(1)
}
printf("************************************\n")
printf("%-10s%-15s%-15s\n","id","name","department")
printf("************************************\n")
for (i=0i<NUMi++)
{ /*显示全部文件内容*/
fread(&record[i],sizeof(struct rec),1,fp1)
printf("%-10s%-15s%-15s\n",record[i].id,record[i].name,record[i].department)
}
/*以下进行文件的随机读写*/
fseek(fp1,2*sizeof(struct rec),0)/* 定位文件指针指向第三条记录*/
fwrite(&record[1],sizeof(struct rec),1,fp1)
/* 在第三条记录处写入第二条记录*/
rewind(fp1)/*移动文件指针到文件头*/
printf("************************************\n")
printf("%-10s%-15s%-15s\n","id","name","department")
printf("************************************\n")
for (i=0i<NUMi++)
{ /*重新输出文件内容*/
fread(&record[i],sizeof(struct rec),1,fp1)
printf("%-10s%-15s%-15s\n",record[i].id,record[i].name,record[i].department)
}
fclose(fp1)/*关闭文件*/
scanf("%d",i)
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)