SKY外语计算机学习

标题: 对文件实现写入和读取功能 [打印本页]

作者: 愚昧儒生    时间: 2013-12-9 23:28
标题: 对文件实现写入和读取功能
请问谁能提供一段可以实现对.dat文件(二进制)写入和读取功能的代码。
本人正在学习怎样向文件实现写入后读取,希望有大神能提供帮助。
作者: admin    时间: 2013-12-16 17:01
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. using namespace std;

  6. struct test
  7. {
  8.     string name;
  9.     int id;
  10.     int age;
  11. };

  12. void print_test(const test &t)
  13. {
  14.     cout << "Name: " << t.name << endl;
  15.     cout << "ID: " << t.id << endl;
  16.     cout << "Age: " << t.age << endl << endl;
  17. }

  18. int main()
  19. {
  20.     test t[2];
  21.     ofstream out("out.dat", ios::out);
  22.     if (!out)
  23.     {
  24.         cerr << "File open error!";
  25.         return 1;
  26.     }

  27.     t[0].name = "Name1";
  28.     t[0].id = 101;
  29.     t[0].age = 20;
  30.     t[1].name = "Name2";
  31.     t[1].id = 102;
  32.     t[1].age = 25;

  33.     out.write((char*)t, sizeof(t));
  34.     out.close();

  35.     ifstream in("out.dat", ios::in);
  36.     if (!in)
  37.     {
  38.         cerr << "File open error!";
  39.         return 1;
  40.     }

  41.     vector<test> vt;
  42.     test tmp;
  43.     while (in.read((char*)&tmp, sizeof(test)))
  44.     {
  45.         vt.push_back(tmp);
  46.         if (in == NULL)
  47.             cout << "NULL";
  48.     }

  49.     vector<test>::iterator it;
  50.     for (it=vt.begin(); it!=vt.end(); ++it)
  51.         print_test(*it);

  52.     in.close();
  53.    
  54.     return 0;
  55. }
复制代码

作者: 愚昧儒生    时间: 2013-12-17 18:29
admin 发表于 2013-12-16 17:01

谢谢,我也写了个
  1. #include<iostream>
  2. #include<fstream>
  3. using namespace std;
  4. struct People
  5. {
  6.         int num;
  7.         char name[18];
  8. };

  9. int main()
  10. {
  11.         People peo[30];
  12.         int num;
  13.         char name[18];
  14.         int n;
  15.         cout<<"plese input the whole number"<<endl;
  16.     cin>>n;


  17.         for(int i=0;i<n;i++)
  18.         {
  19.                 cout<<"please input the num of people:"<<i+1<<endl;
  20.                 cin>>peo[i].num;
  21.                 cout<<"please input the name of people:"<<i+1<<endl;
  22.                 cin>>peo[i].name;
  23.         }


  24.         fstream f("peo.dat",ios::out|ios::binary);
  25.         f.close();

  26.         f.open("peo.dat",ios::in|ios::out|ios::binary);
  27.         if(f.fail())
  28.         {
  29.                 cout<<"error for open"<<endl;
  30.                 exit(1);
  31.         }


  32.     f.write((char*)&peo[0],sizeof(People)*n);

  33.         People p;
  34.         f.seekp(0);
  35.         f.read((char*)&p,sizeof(People));
  36.         while(!f.eof())
  37.         {
  38.                 cout<<endl<<p.num<<endl<<p.name<<endl<<endl;
  39.                 f.read((char*)&p,sizeof(People));
  40.         }
  41.         f.close();
  42.         return 0;
  43. }
复制代码





欢迎光临 SKY外语计算机学习 (http://join.skywj.com/) Powered by Discuz! X2.5