C++简单的文件输入输出

一、简单文件的输入输出:
        文件输出:
              1)  必须包含头文件fstream
              2)头文件fstream定义了一个用于处理输出的ofstream类(out file)
              3)  需要声明一个或者多个ofstream变量(对象),并以自己喜欢的方式对其进行命名,条件是遵守常用的命名的规则
              4) 必须使用命名空间std,(using 或者std::方式)
              5)需要将ofstream对象与文件关联起来,方法之一是使用open()方法
              6)使用完文件后,应使用close()将其关闭
              7)可结合使用ofstream对象和<<来输出各种类型的数据(用其对象代替cout)
        所以步骤:
             包含头文件 fstream---- 创建一个ofstream对象----将该ofstream对象同一个文件关联起来----就像使用cout那用那样使用该ofstream  
   eg:      
            #include<iostream>
#include<fstream>
using namespace std;
int main()
{
 char automobile[50];
 int year;
 double a_price;
 double d_price;
  
 cout<<"Enter the make and model of automobile: ";
 cin.getline(automobile,50);
 cout<<"Enter the model year: ";
 cin>>year;
 cout<<"Enter the original asking price: ";
 cin>>a_price;
 d_price=0.913*a_price;
 
 //用cout输出的话
 cout<<fixed;
 cout.precision(2);
 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;
 //下面是 用ofstream ,基本out怎么用就怎么用就行,包括格式化的方法
 ofstream outFile;
 outFile.open("car.txt"); //如果不存在创建,若存在,则清空再输入(默认,可修改)
 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;
}
 
          读取文本文件:
            与上面类似包含头文件fstream, 用ifstream类定义对象,命名空间std,用open与文件关联,close()关闭用ifstream对象和>>读取各种类型的数据,与cin类似,其可用的函数和格式控制也都可以用,
            多了一点就是可结合使用ifstream和eof(),fail()等方法来判断输入是否成功,(那上面的打开失败如何判断)
          #include<iostream>
#include<fstream>
#include<cstdlib>//exit用到这个头文件
using namespace std;
const int SIZE=60;
int main()
{
 char filename[SIZE];
 ifstream inFile;
 cout<<"Enter name of data file: ";
 cin.getline(filename,SIZE); //文件名读入
 inFile.open(filename); //与文件关联
 if(!inFile.is_open()) //打开成功返回true
 {
  cout<<"Could not open the file "<<filename<<endl;
  cout<<"Program terminating.\n";
  exit(EXIT_FAILURE); //用到cstlib头文件
 }
 double value;
 double sum=0.0;
 int count=0;
 
 inFile>>value;
 while(inFile.good()) //读入正确,且未到末尾
 {
  count++;
  sum+=value;
  inFile>>value;            //读入数据,相当于cin
 }
 if(inFile.eof())   //判断是否结束
  cout<<"End of file reached.\n";
 else if(inFile.fail())              //读入数据不符
  cout<<"Input terminated by data mismatch.\n";
 else
  cout<<"Input terminated for unknow reason.\n";
 if(count==0)
  cout<<"No data processed.\n";
 else
 {
  cout<<"Items read: "<<count<<endl;
  cout<<"Sum: "<<sum<<endl;
  cout<<"Average: "<<sum/count<<endl;
 }
 inFile.close();
 return 0;
}
 
                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值