【Oracle】使用bbed手动提交事务
时间:2022-03-10 17:32
ifstream in(ifile); //构造一个ifstream并打开给定文件
ofstream out; //输出文件流未关联到任何文件
用fstream代替iostream&
首先这里有一个头文件和一个定义的文件要使用
Sales_data.h
#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,因为fstream是继承了iostream 的
/*
* 功能:用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;
}
吧一个文件里面的内容读取到DOS上面!
/*
* 功能:读取文件
* 时间: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;
}
文件模式
in 以读的方式打开
out 以写的方式打开
app 每次操作前均定位到文件末尾
ate 打开文件后立即定位到文件末尾
trunc 截断文件
binary 以二进制方式进行IO
/*
* 功能:文件模式
* 时间: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;
}
PS:一直对IO流特别是文件流这一块不怎么熟悉,这次我是回了好多了!!!开心,我一定可以玩转IO的,哈哈!!
【Oracle】使用bbed手动提交事务,布布扣,bubuko.com