您的位置:首页 > 编程语言 > C语言/C++

C++中的文件的输入输出函数应用实例

2012-07-13 20:28 666 查看
本程序实现的是对一个结构体数组进行写入磁盘二进制文件,提取其中若干个,修改二进制文件中特定的结构体。先上代码:

// in-out stream.cpp : 定义控制台应用程序的入口点。

//

#include "stdafx.h"

#include "iostream "

#include "fstream"

#include<string>

using namespace std;

struct student

{

 string name;

 int    num;

 char   sex;

}stu[5];

#if 0

void save_to_file()

{

 ofstream outfile("f1.txt");

 if( !outfile )

 { cerr << "open f1.txt error! " << endl;

 exit( 1 );

 }

 char c[80 ];

 cin.getline( c, 80);

 for( int i = 0 ; c[ i ] != '\0'; i ++ )

 {   if( c[ i ] <= 90 && c[ i ] >= 65 || c[ i ] >= 97 && c[ i ] <=122 )

 outfile.put( c[ i ]);

 cout << c[ i ];

 }

 cout << endl;

 outfile.close();

 

}

void get_from_file()

{

 char ch;

 // 定义输入文件流infile

 ifstream infile( "f2.txt" );

 if(! infile )

 { cerr << " open f2.dat error!" << endl;}

 

 //定义输出文件outfile

 ofstream outfile( "f3.txt");

 if(! outfile )

 { cerr << " open f3.dat error!" << endl; }

 // 修改、输出到文件并显示到screem

 while( infile.get( ch ) )

  {

   if( ch <= 122 && ch >= 97 )

   { ch = ch - 32; }

   outfile.put( ch );

   cout << ch;

      }

 cout << endl;

 infile.close();

 outfile.close();

}

#else 1

void save_stu_to_file( student* stu )

{

 ofstream outfile( "stu.dat",ios::binary );

 if( !outfile )

 { cerr << "open error !" << endl;

    abort();

 }

 for( int i = 0; i < 5; i ++ )

  outfile.write(( char * )&stu[ i ],sizeof( stu[ i ] ));

}

/*-------------------由磁盘读入stu0,stu2和stu4内存并显示出---------------------*/

void get_from_file( student *stu )

{

 student stu_1[ 5 ];

 int i ,j = 0;

 ifstream infile( "stu.dat",ios::binary );

 if( !infile )

 { cerr << "open  the file error!" << endl; abort();  }

 for( i = 0; i < 5; i = i + 2  )

 {

  infile.seekg( i * sizeof( stu[ i ]),ios::beg );

  infile.read(( char* ) &stu_1[ i/2 ], sizeof( stu_1[ i/2 ] ));

  j ++;

 }

  

 

 for( int i = 0; i < 3; i ++ )

 {

  cout << "name: " << stu_1[ i ].name << " " << "NO.: " << stu_1[ i ].num << " sex: " << stu_1[ i ].sex << endl;

 }

 infile.close( );

}

/*_________________修改第三个stu——————*/

void rectify(student *stu )

{

 student stu_2;

 ifstream infile("stu.dat",ios::binary );

 if( !infile )

 { cerr << "open error in rectify !" << endl; }

 /*------------------------------------*/

 //将文件中的第三个结构体成员读入stu_2中并输出

  infile.seekg( 2 * sizeof( student ));

 infile.read( ( char* )&stu_2, sizeof( stu[ 2 ]));

 

 cout << "stu_2name: " << stu_2.name << " " << "stu_2NO.: " << stu_2.num << " stu_2sex: " << stu_2.sex << endl;

   // 输出结束

 /*-----------修改stu_2成员值-------------------------*/

 stu[ 2 ].name = "Lucy";

 stu[ 2 ].num  =  999;

 stu[ 2 ].sex  =  'u';

 /*--------------将stu[ 2 ]重新写入文件中-------------------*/

 ofstream outfile( "stu.dat",ios::binary );

 if(! outfile )

  { cerr << "open error in rectify !" << endl; }

 cout << "rectify the third (3) NO.: " << endl;

 /*----------特定区域写入必须同时使用outfile.seekp()和outfile.write()--------------*/

 outfile.seekp( 2 * sizeof( student ),ios::beg );

 outfile.write( ( char* )&stu[ 2 ], sizeof( stu[ 2 ] ) );

 /*----------像一对孪生兄弟,写入的stu[2]不可换为stu_2结构体------------------------*/

 infile.seekg( 0, ios::beg );

 /*infile.seekg( 2* sizeof(stu[0]),ios::beg );

 infile.read(( char* )&stu[2],sizeof(stu[ 2 ] ));

 cout << "stu为:" << stu[ 2 ].name << endl;*/

 /*-----------再输出stu文件------------*/

 for( int i = 0; i < 5; i ++ )

 {

  infile.read(( char *)&stu[ i ],sizeof( stu[ i ]));

  cout << "name: " << stu[ i ].name << " " << "NO.: " << stu[ i ].num << " sex: " << stu[ i ].sex << endl;

 }

 infile.close();

 outfile.close();

 

}

#endif

 

int _tmain(int argc, _TCHAR* argv[])

{

 student stu[ 5 ] = {

                  {"wang", 1001,'f'},

                     {"zhang",1002,'f'},

                     {"li" ,  1003,'m'},

                     {"sun",  1004,'f'},

                     {"zhou", 1005,'m'}

                     };

 /*save_to_file();

 get_from_file();*/

 /*------将结构体数组存入磁盘---------*/

 save_stu_to_file( stu );

 //

 /*---------read第1,3,5个结构体成员-----------*/

 //get_from_file( stu );

 /*-----------修改第三个结构体成员值并输出-------------------*/

 //student stu0[ 5 ];

 ifstream infile( "stu.dat",ios::binary );

 /*for( int i = 0; i < 5; i ++ )

 {

  infile.read( (char*)&stu0[ i ],sizeof(stu0[ i ]));

 }

 for( int j = 0 ;j < 5; j ++ )

  cout << stu0[ j ].name << " " << stu0[ j ].num << " " << stu0[ j ].sex << endl;*/

#if 1

 rectify( stu );

#else

 student stu_2;

 stu[ 2 ].name = "Lucy";

 stu[ 2 ].num  =  999;

 stu[ 2 ].sex  =  'u';

 /*--------------将stu_2重新写入文件中-------------------*/

 ofstream outfile( "stu.dat",ios::binary );

 if(! outfile )

  { cerr << "open error in rectify !" << endl; }

 cout << "rectify the third (3) NO.: " << endl;

 outfile.seekp( 2 * sizeof( stu[ 0 ] ),ios::beg );

 outfile.write( ( char* )&stu[ 2 ], sizeof( stu[ 2 ] ) );

 infile.seekg( 0, ios::beg );

 for( int i = 0; i < 5; i ++ )

 {

  infile.read( (char*)&stu[ i ],sizeof(stu[ i ]));

 }

 for( int j = 0 ;j < 5; j ++ )

  cout << stu[ j ].name << " " << stu[ j ].num << " " << stu[ j ].sex << endl;

#endif

 return 1;

}

//可以用txt和dat格式的文件作为输出输入文件的格式。它支持数字和字符。比C中的输出入文件函数功能要好!

//文件中空格也为内容,读取时同其他数据,终止判据为文件终止。

运行结果如下:


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ ios file iostream 磁盘 c