写对象
#include <fstream.h>
#include <stdio.h>
const int LEN=80;
class Book
{
public:
void getdata()
{
cout<<"Enter book title: ";
cin>>title;
cout<<"Enter book author: ";
cin>>author;
cout<<"Enter book sold per month: ";
cin>>numsold;
}
private:
char title[LEN];
char author[LEN];
int numsold;
};
int main(int argc, char *argv[])
{
ofstream output("IO.txt");
Book A;
char ch;
do
{
A.getdata();
output.write((char *) &A,sizeof(A));
cout<<"Enter another one? (y/n): ";
cin>>ch;
}while(ch=='y');
return 0;
}
=========================================================
读写 Vector
#include <iostream>
#include <fstream>
#include <iterator>
#include <vector>
#include <algorithm>
using namespace std;
template<typename T>
struct Point {
T x,y;
explicit Point(const T& x=0, const T& y=0) : x(x),y(y) {}
};
template<typename T>
ostream& operator<< (ostream& os, const Point<T>& p)
{
os << ' ' << p.x << ' ' << p.y;
return os;
}
template<typename T>
istream& operator>> (istream& is, Point<T>& p)
{
is >> p.x >> p.y;
return is;
}
int main()
{
//从文件中读出数据
ifstream fin("save.txt");
vector<Point<int> > v;
copy(istream_iterator<Point<int> >(fin),
istream_iterator<Point<int> >(),
back_inserter(v) );
//打印数据
copy(v.begin(), v.end(),
ostream_iterator<Point<int> >(cout,"/n"));
//存入文件
ofstream fout("out.txt");
copy(v.begin(), v.end(),
ostream_iterator<Point<int> >(fout,"/n"));
cout << "Press ENTER to exit.";
cin.get();
}
#include <iostream.
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
ifstream fopen("open.txt");
ofstream fsave('save.txt");
if(!fopen||!fsave)
{
cerr<<"unable to open file "<<endl;
return -1;
}
// 将文件数据读到vector
vector<int> v( istream_iterator<int>(fopen), istream_iterator() );
//存入文件
copy( v.begin(), v.end(), ostream_iterator<int>(fsave, " ") );
return 0;
}
================================================
基本读写操作
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char fileName[30],name[30];
int number,score;
ofstream outstuf;
cout<<"Please input the name of students file: /n";
cin>>fileName;
outstuf.open(fileName,ios::out);
if(!outstuf)
{
cerr<<"File could not be opened!"<<endl;
abort();
}
outstuf<<"This is a file of students .../n";
cout<<"Input the number,name,and score : (Enter Ctrl+z to end input)/n?";
while(cin>>number>>name>>score)
{
outstuf<<number<<'/t'<<'/"'<<name<<'/"'<<'/t'<<score<<'/n';
cout<<'?';
}
outstuf.close();
return 0;
}
=============================================================
格式读取
#include <iostream>
#include <fstream>
using namespace std;
void main()
{
char *lp[2];
int i = 0;
char s[10]="S";
ifstream in("in.txt");
while(!in.eof())
{
char order[10];
char content1[30];
char content2[30];
char other[10];
in.getline(order,sizeof(order),'(');
if(strcmp(s,order)==0)
{
in.getline(content1,sizeof(content1),',');
in.getline(content2,sizeof(content2),')');
i=*content1-48;
lp[i]=content2;
cout<<"lp["<<i<<"] "<<lp[i]<<endl;
}
in.getline(other,sizeof(other),'/n');
}
in.close();
}
==================================================
链表,IO
#include <iostream.h>
#include <fstream.h>
struct data
{
int id;//登记号
int num;//书分类号
char name[10];
float price;
char date[10];
char press[20];
int buyn;
int lendn;
data *next;
};
void show(ofstream outf, data *head) //显示内容
{
data *p;
p=head;
outf<<"/nNow ,Thes records are:/n";
if (head!=NULL)
do
{
outf<<"id: "<<p->id<<"/n"
<<"num: "<<p->num<<"/t"
<<"name: "<<p->name<<"/t"
<<"price:"<<p->price<<"/t"
<<"date: "<<p->date<<endl
<<"press:"<<p->press<<endl
<<"buyn: "<<p->buyn<<"/t"
<<"lendn:"<<p->lendn<<endl<<endl;
p=p->next;
}while (p!=NULL);
}
data *insert(data *head,data *da)//输入数据
{
data *p0, *p1, *p2;
p1=head;
p0=da;
if (head == NULL)
{
head = p0;
p0->next=NULL;
}
else
{
while ((p0->id > p1->id)&&p1->next!=NULL)
{
p2 = p1;
p1 = p1->next;
}
if (p0->id <= p1->id)
{
if (head == p1)
{
head = p0;
p0->next = p1;
}
else
{
p2->next = p0;
p0->next=p1;
}
}
else
{
p1->next = p0;
p0->next = NULL;
}
}
return head;
}
void main()
{
ofstream outf("book.txt");
if (!outf)
cout<<"can't open file./n";
data *head,*p0;
int n=0;
head = NULL;
for (;;)
{
n++;
cout<<"Input the "<<n<<" data:/n";
p0=new data;
cout<<"id: ";
cin>>p0->id;
if (p0->id==0)
{
delete [] p0;
break;
}
cout<<"num: "; cin>>p0->num;
cout<<"name: "; cin>>p0->name;
cout<<"price:"; cin>>p0->price;
cout<<"data: "; cin>>p0->date;
cout<<"press:"; cin>>p0->press;
cout<<"buyn: "; cin>>p0->buyn;
cout<<"lendn:"; cin>>p0->lendn; ;
head = insert(head,p0); //调用函数插入数据
cout<<endl;
}
show(outf, head); //调用函数把数据写进文件
outf.close();
ifstream inf("book.txt"); //输出到显示器
if (!inf)
cout<<"can't open the file./n";
char c;
cout<<"/n/nthe bookdata is:/n/n";
while (inf.get(c))//显示文件的数据到显示器
cout<<c;
cout<<endl;
inf.close();
}