C++的IO操作

写对象
#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();
}

 一、ASCII 输出   为了使用下面的方法, 你必须包含头文件(译者注:在标准C++中,已经使用取 代,所有的C++标准头文件都是无后缀的。)。这是 的一个扩展集, 提供有缓 冲的文件输入输出操作. 事实上, 已经被包含了, 所以你不必包含所有这两个 文件, 如果你想显式包含他们,那随便你。我们从文件操作类的设计开始, 我会讲解如何进行ASCII I/O 操作。如果你猜是"fstream," 恭喜你答对了! 但这篇文章介绍的方法,我们分别使用"ifstream"?和 "ofstream" 来作输入输出。   如果你用过标准控制台流"cin"?和 "cout," 那现在的事情对你来说很简单。 我们现在开始讲输出部 分,首先声明一个类对象。 ofstream fout;   这就可以了,不过你要打开一个文件的话, 必须像这样调用ofstream::open()。 fout.open("output.txt");   你也可以把文件名作为构造参数来打开一个文件. ofstream fout("output.txt");   这是我们使用的方法, 因为这样创建和打开一个文件看起来更简单. 顺便说一句, 如果你要打开的文 件不存在,它会为你创建一个, 所以不用担心文件创建的问题. 现在就输出到文件,看起来和"cout"的操 作很像。 对不了解控制台输出"cout"的人, 这里有个例子。 int num = 150; char name[] = "John Doe"; fout << "Here is a number: " << num << " "; fout << "Now here is a string: " << name << " ";   现在保存文件,你必须关闭文件,或者回写文件缓冲. 文件关闭之后就不能再操作了, 所以只有在你 不再操作这个文件的时候才调用它,它会自动保存文件。 回写缓冲区会在保持文件打开的情况下保存文 件, 所以只要有必要就使用它。回写看起来像另一次输出, 然后调用方法关闭。像这样: fout << flush; fout.close();    现在你用文本编辑器打开文件,内容看起来是这样:   Here is a number: 150 Now here is a string: John Doe   很简单吧! 现在继续文件输入, 需要一点技巧, 所以先确认你已经明白了流操作,对 "<>" 比较熟悉了, 因为你接下来还要用到他们。继续…   二、ASCII 输入   输入和"cin" 流很像. 和刚刚讨论的输出流很像, 但你要考虑几件事情。在我们开始复杂的内容之前 , 先看一个文本:   12 GameDev 15.45 L This is really awesome!   为了打开这个文件,你必须创建一个in-stream对象,?像这样。 ifstream fin("input.txt");   现在读入前四行. 你还记得怎么用"<<" 操作符往流里插入变量和符号吧?好,?在 "<>" (提取) 操作符. 使用方法是一样的. 看这个代码片段.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值