您的位置:首页 > 其它

第14周-阅读项目4-二进制文件和字符串流操作的一般方法

2015-06-15 19:01 543 查看
问题及代码:

#include<iostream>
#include <fstream>
#include<cstdlib>
#include<cstring>
using namespace std;
struct student
{
    int num;
    char name[20];
    float score;
};
int main( )
{
    int i;
    student stud[5]={1001,"Li",85,1002,"Fun",97.5,1004,"Wang",54,1006,"Tan",76.5,1010,"ling",96};
    fstream iofile("stud.dat",ios::in|ios::out|ios::binary);
    //用fstream类定义输入输出二进制文件流对象iofile
    if(!iofile)
    {
        cerr<<"open error!"<<endl;
        abort( );
    }
    //(1)向磁盘文件输出5个学生的数据并显示出来
    cout<<"(1)向磁盘文件输出5个学生的数据并显示出来"<<endl;
    for(i=0;i<5;i++)
    {
        iofile.write((char *)&stud[i],sizeof(stud[i]));
        cout<<stud[i].num<<" "<<stud[i].name<<" "<<stud[i].score<<endl;
    }

    //(2)将磁盘文件中的第1,3,5个学生数据读入程序,并显示出来;
    cout<<"(2)将磁盘文件中的第1,3,5个学生数据读入程序,并显示出来"<<endl;
    student stud1[5];                  //用来存放从磁盘文件读入的数据
    for(i=0;i<5;i+=2)
    {
        iofile.seekg(i*sizeof(stud[i]),ios::beg);  //定位于第0,2,4学生数据开头
        iofile.read((char *)&stud1[i/2],sizeof(stud1[0]));
        //先后读入3个学生的数据,存放在stud1[0],stud[1]和stud[2]中
        cout<<stud1[i/2].num<<" "<<stud1[i/2].name<<" "<<stud1[i/2].score<<endl;
        //输出stud1[0],stud[1]和stud[2]各成员的值
    }
    cout<<endl;

    //(3) 将第3个学生的数据修改后存回磁盘文件中的原有位置。
    cout<<"(3)将第3个学生的数据修改后存回磁盘文件中的原有位置"<<endl;
    stud[2].num=1012;                         //修改第3个学生(序号为2)的数据
    strcpy(stud[2].name,"Wu");
    stud[2].score=60;
    iofile.seekp(2*sizeof(stud[0]),ios::beg);   //定位于第3个学生数据的开头
    iofile.write((char *)&stud[2],sizeof(stud[2])); //更新第3个学生数据
    iofile.seekg(0,ios::beg);                       //重新定位于文件开头

    //(4)从磁盘文件读入修改后的5个学生的数据并显示出来。
    cout<<"(4)从磁盘文件读入修改后的5个学生的数据并显示出来"<<endl;
    for(i=0;i<5;i++)
    {
        iofile.read((char *)&stud[i],sizeof(stud[i]));  //读入5个学生的数据
        cout<<stud[i].num<<" "<<stud[i].name<<" "<<stud[i].score<<endl;
    }
    iofile.close( );
    return 0;
}


运行结果:



知识点总结:

二进制文件和字符串流操作的一般方法。

学习心得:

真是伤不起,这个程序一运行就破坏了我CB中的的中文字符识别。。

然后我重新调好字符集了再试。。CB又崩溃了。。

(⊙o⊙)…所以我决定不冒险了。。

反正我知道这个程序是个什么意思了。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: