文件的读写(fin && fout)

来源:互联网 发布:mac视频播放器加速 编辑:程序博客网 时间:2024/05/18 09:25

如何让键盘输入字符保存在.txt文件中
如何让我们自己在键盘上输入的字符不仅仅在屏幕上显示,而且还能保存在特定路径的文件中,这让简单枯燥的控制台命令程序变得略有趣。

  1. 首先,先看看cin和cout对象,cin和cout对象使得我们能够从键盘上获取字符,并在屏幕上显示出来。

    • cin对象的作用可以理解成实现:【键盘–>缓冲区】
      如:string str; cin>>str;

    • cout对象的作用可以理解成:【缓冲区–>屏幕上显示】
      如:cout>>str;
      该语句与上述语句实现,将字符串str中的内容送值屏幕上显示。


2.再看看fin和fout对象

  • fout对象的作用可以理解成:向文件中进行写操作 【从缓冲区–>硬盘】

  • fin对象的作用可以理解成:对文件中的内容进行读操作 【从硬盘–>缓冲区】


下面通过一小段代码进行测试:

#include "stdafx.h"#include <iostream>#include <fstream>#include <cstdlib>#include <string>#include <iomanip>using std::cin;using std::cout;using std::endl;using std::ifstream;using std::ofstream;using std::ios;using std::cerr;using std::string;using std::setw;const char *file="D:\\guests.txt";int _tmain(int argc, _TCHAR* argv[]){    char ch;    cout<<"Enter Y/N if you want to clear the "<<file<<" file: "<<endl;    if(cin.get()=='Y'){        ofstream fout;        fout.open(file);        fout.close();    }    else{        cin.get(); // 获取当前输入流中的字符!!!        ifstream fin;        fin.open(file);//当前目录文件"guests.txt"        if(fin.is_open()){            cout<<"Here are the current contents of the "                <<file<<"file: "<<endl;            while(fin.get(ch))                cout<<ch;            fin.close();        }        //add new names        ofstream fout;        fout.open(file,ios::out|ios::app);//ios::app格式实现在文件末尾进行追加内容        if(!fout.is_open()){            cerr<<"Can't open "<<file<<"file for output."<<endl;            exit(EXIT_FAILURE);        }        cout<<"Enter guest names:"<<endl;        string name;        while(getline(cin,name) && name.size()>0){            fout<<name<<endl; //向文件中进行写操作  【从缓冲区-->硬盘】        }        fout.close();        //Show revised file        fin.clear();        fin.open(file);        if(fin.is_open()){            cout<<"Here are the new contents of the "                <<file<<" file: "<<endl;            while(fin.get(ch))                cout<<ch; //对文件中的内容进行读操作  【从硬盘-->缓冲区】            fin.close();        }    }    cout<<endl<<"Done!"<<endl;      return 0;}

运行示例图:
第一次运行结果:

Enter Y/N if you want to clear the D:\guests.txt file:NHere are the current contents of the D:\guests.txtfile:Enter guest names:RAOZIBINLUSHANSHANHere are the new contents of the D:\guests.txt file:RAOZIBINLUSHANSHANDone!请按任意键继续. . .

第二次运行结果:

Enter Y/N if you want to clear the D:\guests.txt file:NHere are the current contents of the D:\guests.txtfile:RAOZIBINLUSHANSHANEnter guest names:BEIJINGSHANGHAIWUHANHere are the new contents of the D:\guests.txt file:RAOZIBINLUSHANSHANBEIJINGSHANGHAIWUHANDone!请按任意键继续. . .
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 手机欠费不交有什么后果 目不交睫 手工活不交押金2019免费供料 心肾不交吃什么中成药 两腮无肉不可交 不交物业费会怎么样 社保交满15年三种情况领不到 手工活不交押金2018免费供料 与朋友交而不信乎 千万不要去西交利物浦 大学该不该上课交手机 不交物业费被起诉后果 房子不住需要交物业费吗 毛坯房交房不合格图片 房产证不满2年交多少税 随身wifi要不要交网费 交不起房租就用妻子东凛 单位交社保 归不归为什么和吴勉绝交 咪表停车不交钱会怎样 金融服务费可以不交吗 2018新规六种情况不用交物业费 贷款买车哪些钱不用交 实名制手机号欠费不交后果 2019新规六种情况不用交物业费 2018新规六种情况不用交暖气费 人不知而不愠不亦君子乎解释 不顾一切居筱亦 人不知而不愠不亦君子乎意思 人不知而不愠不亦君子 人不知而不愠不亦君子乎 神秘爹地惹不起陆亦臣萧玖 神秘爹地惹不起陆亦臣 神秘爹地惹不起萧玖陆亦臣 天不变道亦不变 光年男神不好追江亦寒 萧玖陆亦辰巜神秘爹坨惹不起 不亦君子乎上一句 你若不惜我亦不爱 不亦君子乎 萧玫陆亦臣神秘爹地惹不起