第十四周项目1 - 小玩文件(1)

来源:互联网 发布:淘宝怎么创建链接 编辑:程序博客网 时间:2024/06/05 12:51

(1)下面程序的功能是统计文本文件abc.txt中的字符个数,请填空将程序补充完整。

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. #include <iostream>  
  2. #include <cstdlib>  
  3. #include _____________ // (1)  
  4. using namespace std;  
  5. int main()  
  6. {  
  7.     fstream file;  
  8.     file.open("abc.txt", _________); // (2)  
  9.     if(!file) {  
  10.         cout<<"abc.txt can’t open."<<endl;  
  11.         exit(1);  
  12.     }  
  13.     char ch;  
  14.     int i=0;  
  15.     while( _____________) // (3)  
  16.     {  
  17.         file.get(ch);  
  18.          _____________; // (4)  
  19.     }  
  20.     cout<<"Character: "<<i<<endl;  
  21.     file._____________;// (5)  
  22.     return 0;  
  23. }  
填充如下:

#include <iostream>#include <cstdlib>#include <fstream>// (1)using namespace std;int main(){    fstream file;    file.open("abc.txt",ios::out); // (2)    if(!file)    {        cout<<"abc.txt can’t open."<<endl;        exit(1);    }    char ch;    int i=0;    while(!file.eof()) // (3)    {        file.get(ch);        ++i; // (4)    }    cout<<"Character: "<<i<<endl;    file.close();// (5)    return 0;}
心得:

其实有个纠结的地方。。。刚开始要打开文件时是要ios::out还是ios::in啊?都试了试完全没区别啊
图片:


0 0