zoukankan      html  css  js  c++  java
  • c++之输出文件和输入文件的处理

      各种计算机应用系统通常把一些相关信息组织起来保存在外存储器中,

     称为文件,并用一个名字(称为文件名)加以标识

    C++把文件看成无结构的字节流,

    编码方式: 文本文件      二进制文件

    存取方式: 顺序文件      随机文件

    ifstream、ofstream 和 fstream 类用于内存与文件之间的数据传输

     1.文件操作的基本步骤:

    (1).打开文件

    (2).读/写文件

    (3).关闭文件

    (1).打开文件:

      包括建立文件流对象;与外部文件关联;指定文件的打开方式

    打开文件有两种方法:

      首先建立流对象,然后调用fstream::open()函数连接外部文件

      流类  对象名 ;

      对象名 . open ( 文件名 , 方式 ) ;

      调用流类带参数的构造函数,建立流对象的同时连接外部文件

      流类  对象名 ( 文件名 , 方式 ) ;

    流类可以使用 ifstream、ofstream、fstream;

    open函数的原型:void open (const char* ,int mode,int =filebuf::openprot);

    1.其中,第一个参数表示相关联的文件名

    2.第二个参数表示文件的打开方式

    标识常量

    意义

    ios::in

    0x0001

    读方式打开文件

    ios::out

    0x0002

    写方式打开文件

    ios::ate

    0x0004

    打开文件时,指针指向文件尾

    ios::app

    0x0008

    追加方式

    ios::trunc

    0x0010

    删除文件现有内容

    ios::nocreate

    0x0020

    如果文件不存在,则打开操作失败

    ios::noreplace

    0x0040

    如果文件存在,则打开操作失败

    ios::binary

    0x0080

    二进制方式打开,默认为文本方式

    3.第三个参数是文件的保护方式,一般只用缺省值

      filebuf::openprot  适应MS-DOS 模式

      filebuf::sh_compat  适应MS-DOS 模式
      filebuf::sh_none   无模式
      filebuf::sh_read  读模式
      filebuf::sh_write  写模式

      filebuf、ifstream、ofstream、fstream的构造函数具有相同的参数和缺省值

    文件流的构造函数和 open ( ) 函数用于打开文件,析构函数在流对象被删除之前关闭文件

      用第一种方式打开文件:

       打开一个已有文件datafile.dat,准备读:

        ifstream infile ;  // 建立输入文件流对象

        infile.open( "datafile.dat" , ios::in ) ;  // 连接文件,指定打开方式

       打开(创建)一个文件newfile.dat,准备写:

        ofstream outfile ;  // 建立输出文件流对象

        outfile.open( "d:\newfile.dat" , ios::out ) ;     // 连接文件,指定打开方式

    第二种方法  调用文件流类带参数构造函数 :

      ifstream infile ( "datafile.dat" , ios::in ) ;

      ofstream outfile ( "d:\newfile.dat" , ios::out );

      fstream rwfile ( "myfile.dat" , ios::in | ios::out ) ; 用或运算符 “|” 连接两个表示打开方式的标识常量

    2.关闭文件

      关闭文件操作包括把缓冲区数据完整地写入文件,添加文件结束标志,

        切断流对象和外部文件的连接

      若流对象的生存期没有结束,可以重用
      当一个流对象的生存期结束,系统也会自动关闭文件 
    1 例如:
    2         ofstream  ofile ;        // 创建输出文件流
    3         ofile . open ( "myfile1" ) ;    // ofile流与文件“myfile1”相关联,等价于Ofstream ofile(“myfile1”);
    4         ……            // 访问文件“myfile1”
    5         ofile . close ( ) ;// close函数关闭文件“myfile1”,但流对象任然存在          
    6         ofile . open ( "myfile2" );     // 重用ofile流
    7             

    2.open函数和close函数的应用:

     1 #include <iostream>
     2 #include <fstream>
     3 using namespace std;
     4 
     5 void main()
     6 {
     7     ofstream ost;    //创建输出流对象
     8     ost.open("d:\xiaoxue");//建立文件关联,缺省为文件模式
     9     ost << 20 << endl;    //向流插入数据
    10     ost << 3.5 << endl;
    11     ost.close();    //关闭文件
    12     ifstream ist("d:\xiaoxue");    ;//创建输入流并建立文件关联,
    13     int n;
    14     double d;
    15     ist >> n >> d;    //从流提取数据
    16     cout << n  <<  '	' << d << endl;    //向预定义流插入数据
    17 }

    3.文本文件:

      文本文件用默认方式打开
      文本文件用文本文件流进行读/写操作
      文本文件是顺序存取文件
      描述一个对象的信息称为一个记录
      文本文件本身没有记录逻辑结构
      通常一个逻辑记录用换行符分隔;数据项之间可以用空白符、

       换行符、制表符等分隔

     1 #include <iostream>
     2 #include <fstream>
     3 #include <iomanip>
     4 using namespace std;
     5 
     6 void main()
     7 {
     8     ofstream ost("d:\my.txt");//默认方式打开文本文件
     9     ost << "1234567890" << endl;    //插入字符串
    10     int a = 123;//把整形数转换成字符串
    11     ost << a << endl;
    12     
    13     //以指定格式插入数据
    14     ost << setw(10) << a << endl;
    15     //关闭右对齐,设置左对齐,设置填充符号,设置域宽,
    16     ost << resetiosflags(ios::right) << setiosflags(ios::left) << setfill('*') << setw(10) << a << endl;
    17     //关闭左对齐,设置右对齐,设置精度,设置域宽
    18     ost << resetiosflags(ios::left) << setiosflags(ios::right) << setprecision(6) << setw(10) << 12.34567890 << endl;
    19     //关闭文件
    20     ost.close();
    21 }

    复制文件:

     1 #include <fstream>
     2 #include <iostream>
     3 using namespace std;
     4 //复制文件
     5 int main()
     6 {
     7     ifstream ist("d:\test.txt");
     8     if(!ist)
     9     {
    10         cout << "Connot open 'text' of input"<<endl;
    11         return 0;
    12     }
    13     ofstream ost("d:\test1.txt");
    14     if(!ost)
    15     {
    16         cout << "Connot open file!" << endl;
    17         return 0;
    18     }
    19     char ch;
    20     while(ist && ist.get(ch))
    21     {
    22         ost.put(ch);
    23     }
    24     ost.close();
    25     ist.close();
    26     return 1;
    27 }

    在文件结尾插入字符串:

    1 #include <fstream>
    2 using namespace std;
    3 void main()
    4 {
    5     ofstream ost("d:\test1.txt",ios::app);//追加方式
    6     char s[20] ="	Straightforward!";
    7     ost << s;    //插入字符串
    8     ost.close();
    9 }

    建立一个包含学生成绩、学号、和姓名的文件,并输出,最高分、最低分、平均分等其他存入的信息

     1 #include <iostream>
     2 #include <fstream>
     3 using namespace std;
     4 
     5 void main()
     6 {
     7     char fileName[30],name[30],num[30],title[50];
     8     int n=0;
     9     double max,min,ave,total=0,score;
    10     cout << "Please input the name of students file!"<< endl;
    11     cin >> fileName;
    12     ofstream ost(fileName,ios::out);
    13     if(!ost)
    14     {
    15         cout<< "File could not be open!"<<endl;
    16     }
    17     ifstream ist(fileName,ios::in);
    18     if(!ist)
    19     {
    20         cout << "File could not be open!" << endl;
    21     }
    22     ost << "						学生成绩文件
    ";
    23     cout << "Input num、name and score:(Enter Ctrl-Z to end input.)
    ";
    24     while(cin >> num >> name >> score)
    25     {
    26         ost << num <<'	' << name << '	' << score << '
    ';
    27     }
    28     ost.close();
    29     ist.getline(title,40);//略去标题行
    30     while(ist >> num >> name >> score)    //提取数据
    31     {
    32         cout << num << '	' << name << '	' << score << endl;
    33         if(n==0)
    34         {
    35             min = max = score;
    36         }else
    37         {
    38             if(score>max)
    39             {
    40                 max = score;
    41             }
    42             if(score<min)
    43             {
    44                 min = score;
    45             }
    46         }
    47         n++;
    48         total +=score;    //统计
    49     }
    50     ave = total/n;
    51     cout<< "maximal is" << max << "
    " << "minimal is" << min << '
    ' << "average is" << ave << endl;
    52     ist.close();
    53 }

    浏览文件

     1 #include <iostream>
     2 #include <fstream>
     3 using namespace std;
     4 void main()
     5 {
     6     void browseFile(char*,int);
     7     char fileName[40];
     8     cout << "Please input name of file!"<<endl;
     9     cin>>fileName;
    10     browseFile(fileName,1);
    11 }
    12 void browseFile(char *fileName,int delLine)
    13 {
    14     ifstream ist(fileName,ios::in);
    15     char s[80];
    16     for(int i=1;i<=delLine;i++)
    17     {
    18         ist.getline(s,80);
    19     }    //不显示开始指定的行数
    20     while(!ist.eof())
    21     {
    22         ist.getline(s,80);//按行读取文件
    23         cout << s << endl;//按行显示文件
    24     }
    25     ist.close();
    26 }
  • 相关阅读:
    Kafka-Docker:使用Docker运行Apache Kafka的步骤
    Apache Kafka Connect
    如何创建Kafka客户端:Avro Producer和Consumer Client
    Kafka排队:Apache Kafka作为消息传递系统
    Kafka Broker | 命令行选项和过程
    Apache Kafka Consumer 消费者集
    Apache Kafka Producer For Beginners
    设置Kafka集群的方法
    Apache Kafka工作流程| Kafka Pub-Sub Messaging
    Linux Ubuntu config source
  • 原文地址:https://www.cnblogs.com/Smart-Du/p/4338016.html
Copyright © 2011-2022 走看看