输入输出流 (文件读写)

来源:互联网 发布:网络玄幻小说典藏版 编辑:程序博客网 时间:2024/06/18 04:57

期末考查:肯定是最基本的

1)how to 读;how to 写;
2)文本文件;二进制文件
ps:生成二进制文件来读写,往一个二进制文件里读写东西。

1. 基本知识

1.1 I/O类库中常用流类

iostream:通用输入输出流和其他输入输出流的基类
fstream:输入输出文件流类

1.2 输入输出单个字符的函数:get函数和put函数

  • get函数
    用处:输入单个字符
    参数:可以是字符或字符的ASCII代码
char ch //定义一个字符型变量chcin.get(ch)

从输入流中读取一个字符,赋给字符变量ch

  • put函数
    用处:输出单个字符
    参数:可以是字符或字符的ASCII代码
cout.put('a')cout.put(97)cout.put(65+32)

在屏幕上显示一个字符a,上面三条语句等价

1.3 eof函数

定义:eof是end of file的缩写,表示“文件结束”。
从输入流读数据,如遇到文件结束符,eof函数值返回真(非0),否则为假 (0)。

while (!cin.eof())

2. 文件读写

读用in,写用out!!

2.1 文本文件 (ASCII码文件) 的操作

1) 读 <=> 从文件中读取数据

// step1: 以读的形式打开文件(读一个现成已有的文件)ifstream infile ("f1.dat",iOS::in|ios::nocreate);// step2: checkif (!infile)    {cerr<<"open error!"<<endl;     exit(1);    }// step3:具体读取内容的时候infile>>a[i];// step4:关闭文件outfile.close();

2) 写<=> 数据输出到文件

// step1: 以写的形式打开文件ofstream outfile ("f1.dat",iOS::out);// step2: checkif (!outfile)    {cerr<<"open error!"<<endl;     exit(1);    }// step3:具体写入内容的时候outfile<<a[i]<<" ";// step4:关闭文件infile.close();

2.1 二进制文件的操作

对二进制只介绍一种情况:既作为输入,也作为输出
1) 读 <=> 从文件中读取数据

// step1: 以读文件形式打开文件ifstream infile ("f1.dat",ios::binary);// step2:checkif (!infile)    {cerr<<"open error!"<<endl;     abort();    }// step3: 具体读取内容的时候infile.read((char *); 字符串首地址,输出字节数)// step4: 关闭文件infile.close();

2) 写<=> 数据输出到文件

// step1: 以写文件形式打开文件ofstream outfile ("f1.dat",ios::binary);// step2:checkif (!outfile)    {cerr<<"open error!"<<endl;     abort();    }// step3: 具体写入内容的时候outfile.write((char *) 字符串首地址,输出字节数)// step4: 关闭文件outfile.close();

3. 例子

输入:3 2 1 6 5 4 9 8 7 0
输出:max=9
order=6

ps:例3、例5待检查

例1: (写文本型文件)从键盘输入10个整数给一个整数数组,将此数组送到磁盘文件中存放

#include <fstream>#include <iostream>using namespace std;int main(){    int a[10];    // step1: 以写文件形式打开文件    ofstream outfile("f1.dat",ios::out);    // step2:check    if(!outfile)    {        cerr<<"open error!"<<endl;        exit(1);    }    for(int i=0;i<10;i++)    {        cin>>a[i];        outfile<<a[i]<<" "; // step3: 具体写入内容的时候    }    // step4: 关闭文件    outfile.close();    return 0;}

例2: (写二进制文件)同例1,用二进制文件实现

#include <fstream>#include <iostream>using namespace std;int main(){    int a[10];    // step1: 以写文件形式打开文件    ofstream outfile ("f1.dat",ios::binary);    // step2:check    if(!outfile)    {        cerr<<"open error!"<<endl;        abort();    }    for(int i=0;i<10;i++)    {        cin>>a[i];        outfile.write((char *) &a[i],sizeof(a[i])); // step3: 具体写入内容的时候    }    // step4: 关闭文件    outfile.close();    return 0;}

例3:(读文本型文件)从例1中已经建立的文件f1.dat中读取10个整数放在数组中,找出并输出其中的最大者及其序号

#include <fstream>#include <iostream>using namespace std;int main(){    int a[10],max,i,order;    // step1: 以读文件形式打开文件    ifstream infile("f1.dat",ios::in);    // step2:check    if(!infile)    {        cerr<<"open error!"<<endl;        exit(1);    }    for(int i=0;i<10;i++)    {        infile>>a[i];  // step3: 具体读入内容的时候        cout<<a[i]<<" ";     }    //与文件读写无关的一段代码    cout<<endl;    max=a[0];    order=0;     for(int i=0;i<10;i++)        if(a[i]>max)        {            max=a[i];            order=i;        }    cout<<"max="<<max<<endl<<"order="<<order<<endl;    // step4: 关闭文件    infile.close();    return 0;}

例4: (读二进制文件)同例2,用二进制文件实现

#include <fstream>#include <iostream>using namespace std;int main(){    int a[10],max,i,order;    // step1: 以读文件形式打开文件    ifstream infile("f1.dat",ios::binary);    // step2:check    if(!infile)    {        cerr<<"open error!"<<endl;        abort();    }    for(int i=0;i<10;i++)    {        infile.read((char *) &a[i],sizeof(a[i]));   // step3: 具体读入内容的时候        cout<<a[i]<<" ";     }    //与文件读写无关的一段代码    cout<<endl;    max=a[0];    order=0;     for(int i=0;i<10;i++)        if(a[i]>max)        {            max=a[i];            order=i;        }    cout<<"max="<<max<<endl<<"order="<<order<<endl;    // step4: 关闭文件    infile.close();    return 0;}

例5: (读写同一个文本型文件)结合例1&例3

//前提是已经有了这么一个数组,可以先创立一个“输入:3 2 1 6 5 4 9 8 7 0 ”#include <fstream>#include <iostream>using namespace std;int main(){    int a[10],max,i,order;    // 【读】step1: 从f5.dat文件中读取数据       ifstream infile("f5.dat",ios::in);    for(int i=0;i<10;i++)    {        infile>>a[i];      }    infile.close();    //【写】step2: 把求出的最大值和序号写入f5.dat文件中    ofstream outfile("f5.dat",ios::out); //打开文件    cout<<endl;    max=a[0];    order=0;    for(int i=0;i<10;i++)        if(a[i]>max)        {            max=a[i];            order=i;        }    for(int i=0;i<10;i++)    {        outfile<<a[i]<<" ";     }    outfile<<"max="<<max<<endl<<"order="<<order<<endl;    outfile.close();    return 0;}

写读写的可能性一

#include <fstream>#include <iostream>using namespace std;int main(){    int a[10],max,i,order;    // 【写】step1: 将原石数据写入f1.dat文件中:3 2 1 6 5 4 9 8 7 0     ofstream outfile("f6.dat",ios::out);    for(int i=0;i<10;i++)    {        cin>>a[i];        outfile<<a[i]<<" ";     }    outfile.close();    // 【读】step2: 从f1.dat文件中读取数据       ifstream infile("f6.dat",ios::in);    for(int i=0;i<10;i++)    {        infile>>a[i];      }    infile.close();    //【写】step3: 把求出的最大值和序号写入f1.dat文件中    ofstream outfile("f6.dat",ios::out); //打开文件    cout<<endl;    max=a[0];    order=0;    for(int i=0;i<10;i++)        if(a[i]>max)        {            max=a[i];            order=i;        }    outfile<<"max="<<max<<endl<<"order="<<order<<endl;    outfile.close();    return 0;}

写读写的可能性二:

#include <fstream>#include <iostream>using namespace std;int main(){    int a[10],max,i,order;    // 【写基本数据】step1: 将原石数据写入f1.dat文件中:3 2 1 6 5 4 9 8 7 0     ofstream outfile("f6.dat",ios::out);    for(int i=0;i<10;i++)    {        cin>>a[i];        outfile<<a[i]<<" ";     }    max=a[0];    order=0;    for(int i=0;i<10;i++)        if(a[i]>max)        {            max=a[i];            order=i;        }    // 【写处理数据】step2: 把最大值和order写入文件    outfile<<"max="<<max<<endl<<"order="<<order<<endl;    outfile.close();    return 0;}

例6: (读写同一个二进制文件)结合例2&例4

//对同一个文件进行读写操作的,注意:读和写的代码块要分离!#include <fstream>#include <iostream>using namespace std;int main(){    // step1: 从f1.dat文件中读取数据            int a[10],max,i,order;    ifstream infile("f1.dat",ios::binary);    for(int i=0;i<10;i++)    {        infile.read((char *) &a[i],sizeof(a[i]));       infile.close();    //step2: 把求出的最大值和序号写入f1.dat文件中    ofstream outfile ("f1.dat",ios::binary);    max=a[0];    order=0;    for(int i=0;i<10;i++)        if(a[i]>max)        {            max=a[i];            order=i;        }    outfile.write((char *) &max,sizeof(max));    outfile.write((char *) &order,sizeof(order));    outfile.close();    return 0;}

例7:
eof的用法

#include <iostream>#include <fstream>using namespace std;int main(){    void fileset();    fileset();    void fileread();    fileread();    void filesort();    filesort();    return 0;}//问题一:建立两个文件,从键盘输入20个整数,分别存在两文件中void fileset(){    ofstream outfile1("e1.dat",ios::out),outfile2("e2.dat",ios::out); //同时操作两个文件时    int a[21];    for(int i=1;i<21;i++) cin>>a[i];    //对文件一的输出    for(int m=1;m<11;m++)     {        outfile1<<a[m]<<" ";    }    //对文件二的输出    for(int j=11;j<21;j++)    {        outfile2<<a[j]<<" ";    }    outfile1.close();    outfile2.close();   }//问题二:从第一个文件中读10个数,追加到文件二中void fileread(){    //从文件一从读入数据    ifstream infile("e1.dat",ios::in);    int c[11];    for(int i=1;i<11;i++) infile>>c[i];    //把文件一中的数据追加到文件二中    ofstream outfile("e2.dat",ios::app);    for(int i=1;i<11;i++) outfile<<c[i]<<" ";    infile.close();    outfile.close();    }//问题三:从第二个文件中读20个数,排序后放到原文件中(不保留原数据)void filesort(){    //从文件二中读入数据    int b[21];    void quicksort(int b[]);    ifstream infile("e2.dat",ios::in);    for (int i=1;i<21;i++) infile>>b[i];    quicksort(b);    infile.close();    //从排好序的数据写入文件二    ofstream outfile("e2.dat",ios::out);    for (int i=1;i<21;i++)     {        outfile<<b[i]<<" ";        cout<<b[i]<<" ";    }    outfile.close();    }//问题三中嵌套调用的排序函数void quicksort(int c[]){    for (int i=1;i<20;i++)    {        int max=c[i],m=i;//初始值很关键        for (int j=(i+1);j<21;j++)        {            if (c[j]>max)                  {                    max=c[j];                    m=j;                }        }//最里面一层循环是把i之后元素的最大值及其脚标找到        c[m]=c[i];        c[i]=max;    }//最外层循环的作用是交换元素}

练手

#include <iostream>#include <fstream>using namespace std;int main(){    char a[11];    //把一个字符数组写入文件    cout<<"input a word: ";    for(int i=1;i<11;i++) cin>>a[i];    ofstream outfile("g2.dat",ios::binary);    for(int i=1;i<11;i++) outfile.write((char *) &a[i],sizeof(a[i]));     outfile.close();    //从文件中读入一段长度未知的字符    char b[20];    int j=0;    ifstream infile("g2.dat",ios::binary);    while (!infile.eof()) //核心代码    {        j=j+1;        infile.read((char *) &b[j],sizeof(b[j]));     }          for (int i=1;i<(j+1);i++) cout<<b[i];    cout<<endl;    infile.close();    return 0;}
原创粉丝点击