磁盘文件输入输出练习

  1. 从键盘输入一个字符串,并逐个将字符串的每个字符传送到磁盘文件中,字符串的结束标记为“!”。

#include <fstream>

#include <iostream>

#include <stdlib.h>

using namespace std;

 

int main()

{

    char a[100];

    ofstream outfile("1.txt",ios::out);

    if(!outfile)

    {

        cerr<<"open error!"<<endl;

        exit(1);

    }

    cout<<"enter strings"<<endl;

    int i = 0;

    while(1)

    {

        cin>>a[i];

        if(a[i]=='!'||i==100)

            break;

            outfile<<a[i]<<" ";

        i++;

    }

    outfile.close();

    return 0;

}

 

2. 输出十进制、八进制、十六进制显示的数据0~15。

#include <fstream>

#include <iostream>

using namespace std;

 

int main()

{

    int i;

    for(i = 0;i<=15;i++)

    {

        cout.setf(ios::showbase);

        cout<<"dec:"<<i<<" ";

        cout.unsetf(ios::dec);

        cout.setf(ios::oct);

        cout<<"oct:"<<i<<" ";

         cout.unsetf(ios::oct);

        cout.setf(ios::hex);

        cout<<"hex:"<<i<<endl;

         cout.unsetf(ios::hex);

    }

    return 0;

}

3.建立两个磁盘文件f1.dat和f2.dat,编程实现以下工作:

  1. 从键盘输入16个整数,分别存放在两个磁盘文件中(每个文件存放8个整数);
  2. 从f1.dat读入8个数,然后存放到f2.dat文件原有数据的后面;
  3. 从f2.dat中读入16个整数,将他们按照从小到大的顺序存放到f2.dat(不保存原来的数据)
  4. 分别输出文件f1.dat和f2.dat的内容

#include <fstream>

#include <iostream>

#include <stdlib.h>

#include <algorithm>

using namespace std;

 

int main()

{

    int a[16];

    ofstream outfile1("f1.dat",ios::out);

    if(!outfile1)

    {

        cerr<<"open error!"<<endl;

        exit(1);

    }

    cout<<"enter 16 integer numbers:"<<endl;

    for(int i = 0;i<8;i++)

    {

        cin>>a[i];

        outfile1<<a[i]<<" ";

    }

    outfile1.close();

    ofstream outfile2("f2.dat",ios::out);

    if(!outfile2)

    {

        cerr<<"open error!"<<endl;

        exit(1);

    }

    for(int i = 8;i<16;i++)

    {

        cin>>a[i];

        outfile2<<a[i]<<" ";

    }

    outfile2.close();

    ifstream  infile1("f1.dat",ios::in);

    if(!infile1)

    {

        cerr<<"open f1.dat error!"<<endl;

        exit(1);

    }

    int b[16];

    for(int i = 0;i<8;i++)

    {

        infile1>>b[i];

    }

 

    ifstream  infile2("f2.dat",ios::in);

    for(int i = 8;i<16;i++)

    {

        infile2>>b[i];

    }

    sort(b,b+16);

    infile2.close();

    outfile2.open("f2.dat",ios::out);

    for(int i = 0;i<16;i++)

    {

        outfile2<<b[i]<<" ";

    }

    outfile2.close();

    if(!infile1)

    {

        cerr<<"open f1.dat error!"<<endl;

        exit(1);

    }

    for(int i = 0;i<8;i++)

    {

        infile1>>b[i];

        cout<<b[i]<<" ";

    }

    cout<<endl;

    infile1.close();

    infile2.open("f2.dat",ios::in);

    if(!infile2)

    {

        cerr<<"open f2.dat error!"<<endl;

        exit(1);

    }

    for(int i = 0;i<16;i++)

    {

        infile2>>b[i];

        cout<<b[i]<<" ";

    }

    cout<<endl;

    infile1.close();

    infile2.close();

    return 0;

}

4.将存放在源文件找中的学生成绩读入二维整型数组a[3][5]中,数组a的第0列存放学号,第四列存放平均成绩。计算出每个学生的平均成绩,将数组a按平均成绩升序排列后存放在目的文件中。学生的学号与成绩如数组所示。编写程序实现上述要求。

实验数据:

源文件内容如下:

1001 90 85 80 0

1002 80 70 60 0

1003 85 80 75 0

#include <fstream>

#include <iostream>

#include <stdlib.h>

#include <algorithm>

using namespace std;

 

int main()

{

    int a[3][5];

    ifstream infile1("f2.dat",ios::in);

    if(!infile1)

    {

        cerr<<"open error!"<<endl;

        exit(1);

    }

    for(int i = 0; i<3; i++)

    {

        for(int j = 0; j<5; j++)

        {

            infile1>>a[i][j];

        }

    }

    infile1.close();

    for(int i = 0; i<3; i++)

    {

        a[i][4] = (a[i][1]+a[i][2]+a[i][3])/3;

    }

    int b[5];

    if(a[0][4]>a[1][4])

    {

        for(int i = 0; i<5; i++)

            b[i] = a[0][i];

        for(int i = 0; i<5; i++)

             a[0][i]= a[1][i];

        for(int i = 0; i<5; i++)

            a[1][i] = b[i];

    }

 

    if(a[1][4]>a[2][4])

    {

        for(int i = 0; i<5; i++)

            b[i] = a[1][i];

        for(int i = 0; i<5; i++)

            a[1][i] = a[2][i];

        for(int i = 0; i<5; i++)

            a[2][i] = b[i];

    }

    if(a[0][4]>a[1][4])

    {

        for(int i = 0; i<5; i++)

            b[i] = a[0][i];

        for(int i = 0; i<5; i++)

            a[0][i] = a[1][i];

        for(int i = 0; i<5; i++)

            a[1][i] = b[i];

    }

    ofstream outfile("f1.dat",ios::out);

    if(!outfile)

    {

        cerr<<"open error!"<<endl;

        exit(1);

    }

    for(int i = 0; i<3; i++)

    {

        for(int j = 0; j<5; j++)

        {

            if(j==4)

                outfile<<a[i][j]<<"\n";

            else

                outfile<<a[i][j]<<" ";

        }

    }

    return 0;

}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值