c++简单文件输入/输出介绍

有时候通过键盘输入并不是最好的选择,下面我将介绍写入到文本文件中和读取文本文件两种简单的文件输入输出。

写入到文本文件

1.必须包含头文件iostream。

2.头文件iostream定义了一个用处理输出的 ostream类。

3.头文件iostream声明了一个名为cout的ostream变量(对象)。

4.必须指明名称空间std。例如,为引用cout和endl,必须使用编译指令using或前缀std::。

5.可以结合使用cout和运算符<<来显示各种类型的数据

6.文件输出与其十分相似

7.必须包含头文件fstream。

8.头文件fstream定义了一个用于处理输出的ofstrem类。

9.需要声明一个或多个ofstrem变量(对象),并以自己喜欢的方式对其进行命名,条件是遵守常用的命名规则。

10.必须指明名称空间std;例如,为引用元素ofstream,必须使用编译指令using或前缀std::

11.需要将ofstream对象与文件关联起来。为此,方法之一是使用open()方法。

12.使用完文件后,应使用方法close()将其关闭。

13.可结合使用ofstrem对象和运算符<<来输出各种类型的数据。

 

#include<iostream>
#include<fstream>//需要使用到该头文件
int main()
{
    using namespace std;
    char automobile[50];//字符型数组便于输入名字
    int year;
    double a_price;
    double d_price;
    ofstream outFile;//ofstream类定义的outFile,从命名上可以看出它的功能是将输出的内容写入文件
    outFile.open("ccut2021.txt");//这是要联系起来的文件名,刚开始未创建
    cout<<"Enter the make and model of automobile: ";
    cin.getline(automobile,50);
    cout<<"Enter the model year: ";
    cin>>year;
    cout<<"enter the orginal asking price: ";
    cin>>a_price;
    d_price = 0.913 * a_price;

    cout<<fixed;//用定点表示法表示浮点数!!
    cout.precision(2);//precision(val) 设置val为新的浮点数精度值, 并返回原值
    cout.setf(ios_base::showpoint);
    cout<<"Make and model: "<<automobile<<endl;
    cout<<"Year: "<<year<<endl;
    cout<<"Was asking $"<<a_price<<endl;
    cout<<"Now asking $"<<d_price<<endl;
//以下写入到已生成的文件中
    outFile<<fixed;
    outFile.precision(2);
    outFile.setf(ios_base::showpoint);
    outFile<<"Make and model: "<<automobile<<endl;
    outFile<<"Year: "<<year<<endl;
    outFile<<"Was asking $"<<a_price<<endl;
    outFile<<"Now asking $"<<d_price<<endl;
    
    outFile.close();//文件有打开就有关闭
    return 0;
}

可以发现图一还未生成文件 

 

 程序运行结束后程序就生成了文件,其内容是和输入的内容一样的

 注意:打开已有的文件,以接收输出时,默认将其长度截断为0,因此原来的内容将丢失

 读取文本文件

 

#include<iostream>
#include<fstream>//需要的头文件
#include<cstdlib>//exit
const int SIZE = 60;
int main()
{
    using namespace std;
    char filename[SIZE];//文件名
    ifstream inFile;//ifstream对象定义的inFile,从命名上可以看出他的功能是将文件里面的内容读入
    cout<<"Enter name of data file: ";
    cin.getline(filename,SIZE);
    inFile.open(filename);//打开该文件
    if(!inFile.is_open())//如果打不开,就是不存在
    {
        cout<<"Could not open the file "<<filename<<endl;
        cout<<"Program terminating.\n";
        exit(EXIT_FAILURE);//终止程序
    }
    double value;
    double sum=0.0;
    int count=0;
    
    //inFile>>value;
    while(inFile.good())//一种方法,在没有任何错误时返回true
    {
        inFile>>value;
        ++count;
        sum+=value;
    }
    /*if(inFile.eof())
    cout<<"End of file reached.\n";
    else if(inFile.fail())
    cout<<"Input terminated by date mismatch.\n";
    else
    cout<<"Input terminated for unknown reason.\n";这种方法也可行,更加详细*/
    if(count==0)
    cout<<"NO date processed.\n";
    else
    {
        cout<<"Iteams read:"<<count<<endl;//一共读取的数据,还得加个1,因为在while前已经输入了一个
        cout<<"Sum:"<<sum<<endl;//所有数据的和
        cout<<"Average: "<<sum/count<<endl;//平均数
    }
    inFile.close();
    return 0;

}

这是在文件里输入的数字,计算后与呈现出的结果一致

 

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

forget hurt

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值