文件的读写(fin && fout)

这篇博客介绍了如何利用C++中的fin和fout对象进行文件读写操作。通过键盘输入的字符不仅可以显示在屏幕上,还能被保存到指定路径的.txt文件中。cin和cout用于键盘输入和屏幕输出,而fout则负责将内容写入文件,fin用于从文件读取内容到缓冲区。通过示例代码展示了具体的操作过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如何让键盘输入字符保存在.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:
N
Here are the current contents of the D:\guests.txtfile:
Enter guest names:
RAOZIBIN
LUSHANSHAN

Here are the new contents of the D:\guests.txt file:
RAOZIBIN
LUSHANSHAN

Done!
请按任意键继续. . .

第二次运行结果:

Enter Y/N if you want to clear the D:\guests.txt file:
N
Here are the current contents of the D:\guests.txtfile:
RAOZIBIN
LUSHANSHAN
Enter guest names:
BEIJING
SHANGHAI
WUHAN

Here are the new contents of the D:\guests.txt file:
RAOZIBIN
LUSHANSHAN
BEIJING
SHANGHAI
WUHAN

Done!
请按任意键继续. . .
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值