ifstream in(ifile); //构造一个ifstream并打开给定文件 ofstream out; //输出文件流未关联到任何文件
#ifndef SALES_DATA_H_INCLUDED #define SALES_DATA_H_INCLUDED #include <string> #include <iostream> class Sales_data { friend Sales_data add(const Sales_data&, const Sales_data&); friend std::ostream &print(std::ostream&, const Sales_data&); friend std::istream &read(std::istream&, Sales_data&); public: // constructors Sales_data(): units_sold(0), revenue(0.0) { } Sales_data(const std::string &s): bookNo(s), units_sold(0), revenue(0.0) { } Sales_data(const std::string &s, unsigned n, double p): bookNo(s), units_sold(n), revenue(p*n) { } Sales_data(std::istream &); // operations on Sales_data objects std::string isbn() const { return bookNo; } Sales_data& combine(const Sales_data&); double avg_price() const; private: std::string bookNo; unsigned units_sold; double revenue; }; #endif // SALES_DATA_H_INCLUDED
#include <iostream> using std::istream; using std::ostream; #include "Sales_data.h" Sales_data::Sales_data(std::istream &is) { // read will read a transaction from is into this object read(is, *this); } double Sales_data::avg_price() const { if (units_sold) return revenue/units_sold; else return 0; } // add the value of the given Sales_data into this object Sales_data& Sales_data::combine(const Sales_data &rhs) { units_sold += rhs.units_sold; // add the members of rhs into revenue += rhs.revenue; // the members of ``this'' object return *this; // return the object on which the function was called } Sales_data add(const Sales_data &lhs, const Sales_data &rhs) { Sales_data sum = lhs; // copy data members from lhs into sum sum.combine(rhs); // add data members from rhs into sum return sum; } // transactions contain ISBN, number of copies sold, and sales price istream& read(istream &is, Sales_data &item) { double price = 0; is >> item.bookNo >> item.units_sold >> price; item.revenue = price * item.units_sold; return is; } ostream& print(ostream &os, const Sales_data &item) { os << item.isbn() << " " << item.units_sold << " " << item.revenue << " " << item.avg_price(); return os; }
/* * 功能:用fstream代替iostream& * 时间:2014年6月7日07:58:39 * 作者:cutter_point */ #include"Sales_data.h" #include"Sales_data.cc" #include<fstream> using namespace std; int main() { string argv[3]; ifstream input(argv[1]); //打开销售记录文件 ofstream output(argv[2]); //打开输出文件 Sales_data total; //保存销售总额的变量 if(read(input, total)) //读取一条记录 { //保存下一条销售记录的变量 Sales_data trans; while(read(input, trans)) { //读取剩余的记录 if(total.isbn() == trans.isbn()) total.combine(trans); //更新销售总额 else { print(output, total)<<endl; //打印结果 total=trans; //处理下一本书 } } print(output, total)<<endl; //打印最后一本书 } else cerr<<"No data?!"<<endl; return 0; }
/* * 功能:读取文件 * 时间:2014年6月7日08:51:44 * 作者:cutter_point */ #include<iostream> #include<fstream> #include<string> #include<vector> using namespace std; int main() { vector<string> v1; ifstream infile("C:\\booklist.txt"); char s1[30]; while(infile) { infile.getline(s1,30); v1.push_back(s1); } infile.close(); for(int i=0 ; i<v1.size() ; ++i) { cout<<v1[i]<<endl; } return 0; }
/* * 功能:文件模式 * 时间:2014年6月7日09:06:29 * 作者:cutter_point */ #include<iostream> #include<string> #include<fstream> #include<vector> using namespace std; int main() { ofstream outfile; outfile.open("booklist.txt", ofstream::app); int i=0; while(i != 2) { outfile<<"This is just a kinding!!"<<endl; ++i; } outfile.close(); return 0; }
【足迹C++primer】22、文件输入输出,布布扣,bubuko.com
原文地址:http://blog.csdn.net/cutter_point/article/details/29168033