c++文件操作

“输入文件流”变量的类型名称是ifstream;“输出文件流”变量的类型名称是ofstream。为了将in_stream声明为一个用于文件的输入流,将out_stream声明为用于另一个文件的输出流需要使用如下所示的语句:

ifstream in_stream;
ofstream out_stream;

ifstream和ofstream类型在头文件为fstream的一个库中定义。所以,任何程序要想以这种方式来声明,都必须包含一下预编译指令:

#include <fstream>
using namespace std;

一旦声明了输入流变量,并使用open函数将其连接到一个文件,程序就可以使用提取操作符>>从文件中获取输入。

输出流采用和输入流想相同的方式来打开(也就是连接到一个文件)。例如,以下语句声明了输出流out_stream,并将其连接到一个名为outfile.dat文件:

ofstream out_stream;
out_stream.open("outfile.dat");

针对ofstream类型的一个流,成员函数open会新建一个尚不存在的输出文件。如果输出文件已经存在,成员函数open就会丢弃文件的内容。所以,在调用了open函数之后,输出文件为空。通过调用open将文件连接到out_stream流之后,程序可使用插入操作符<<将输出发送到那个文件。例如,以下语句两个字符串和两个变量one_number与another_number的内容写入一个已经与out_stream流连接的文件(本例是outfile.dat文件):

out_stream <<"one_number="<<one_number
<<"another_number="<<another_number;
一个文件有两个名称:程序使用的每一个输入文件和输出文件都有两个名称。外部文件名是文件的真实名称,但它只在open函数调用中使用,该函数负责将文件连接到一个流。一旦调用了open,就必须将流(对象)的名称当做文件名来使用。

流是一种特殊的变量,称为对象。

//简单的文件输入输出
//从文件infile.dat读取3个数,求和,将结果写入文件outfile.dat
#include <fstream>
int main()
{ using namespace std;
   ifstream in_stream;
   ofstream out_stream;
   
   in_stream.open("infile.dat");
   out_stream.open("outfile.dat");
   int first,second,third;
   in_stream>>first>>second>>third;
   out_stream<<"the sum of the first 3\n"
    <<"number in infile.dat\n"
    <<"is "<<(first+second+third)
    <<endl;
    in_stream.close();
    out_stream.close();
    return 0;
}

**************************************************************************************************************************

编程提示:检查文件是否成功打开

***************************************************************************************************************************

可以使用成员函数fail来测试一个流操作是否失败。ifstream和ofstream类都有一个名为fail的成员函数。fail不取任何参数,返回一个bool值。例如,要为in_stream调用fail函数,可以这样写:in_stream.fail();这是一个布尔表达式,可用于控制while循环或if  else语句。在每个open调用之后,都应该立即调用fail。如果open调用失败,fail函数就会返回true。例如,假定以下open调用失败:

in_stream.open("stuff.dat");
if(in_stream.fail())
{  
    cout<<"Input file opening failed.\n";
    exit(1);//终止程序
 }

程序会输出一条错误信息并终止。如果调用成功,fail函数会返回false,程序得以继续。

exit语句造成程序立即终止。exit函数将它的实参返回给操作系统。为了使用exit函数,程序中必须包含以下include预编译指令:

#inculde <cstdlib>

和using namespace std;

exit 函数是一个预定义函数,它取一个整数作为实参。根据规范,如果因为一个错误而调用exit,就使用1作为实参;否则使用0。在我们的例子中,无论使用哪个整数都没有区别,但最好还是遵守上述约定,因为在高级编程环境中,这一点可能非常重要。

总上:执行exit语句时,程序立即终止。可使用任何整数,但根据约定,如果是因为一个错误而调用exit,就使用1作为实参;否则使用0.该函数在头文件为cstdlib的一个库中,所以使用exit语句的任何程序都必须包含一下预编译指令:
#include<cstdlib>
using namespace std;
#include <fstream>
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{ using namespace std;
   ifstream in_stream;
   ofstream out_stream;
   
   in_stream.open("infile.dat");
   if(in_stream.fail())
   { cout<<"input file opening failed.\n";
     exit(1);
   
    }
   out_stream.open("outfile.dat");
   if(out_stream.fail())
   { cout<<"output file opening failed.\n";
     exit(1);
   
    }
   int first,second,third;
   in_stream>>first>>second>>third;//就像使用cin从键盘获取输入一样,使用in_stream流从infile.dat文件获取输入。
   out_stream<<"the sum of the first 3\n"//就像使用cout将输出发送到屏幕一样,使用out_stream流将输出发送到outfile.dat文件。
    <<"number in infile.dat\n"
    <<"is "<<(first+second+third)
    <<endl;
    in_stream.close();
    out_stream.close();
    return 0;
}

*********************************************************************************************************************************

从文件中输入字符,输出字符到文件

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{cout<<"Enter a line of input and i will echo it :\n";
 char symbol;
 ifstream infchar;
 infchar.open ("test.dat");
 if(infchar.fail ())
 { cout<<"input file opening failed.\n";
   exit(1);
  }
 ofstream outfchar;
 outfchar.open ("result.dat");
 if(infchar.fail())
 { cout<<"result file opening failed.\n";
   exit(1);
 }
 do 
 { infchar.get(symbol);
   outfchar.put(symbol);
  }while(symbol!='\n');
 cout<<"that is all for the demonstration.\n";
 infchar.close();
 return 0;
}


 

//test.dat
I am Chinese.


****************************************************************************************************

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
void add_plus_plus(ifstream& in_stream,ofstream& out_stream);
int main()
{ ifstream fin;
  ofstream fout;
  cout<<"Begin editing files.\n";
  fin.open ("cad.dat");
  if(fin.fail())
  { cout<<"Input file opening failed.\n";
    exit(1);
  }
  fout.open("cplusad.dat");
  if(fout.fail ())
  { cout<<"Output file opening failed.\n";
  exit(1);
  }
  add_plus_plus(fin,fout);
  fin.close();
  fout.close();
  cout<<"end of editing files.\n";
  return 0;
}
void add_plus_plus(ifstream& in_stream,ofstream& out_stream)
{ char next;
  in_stream.get(next);
  while(!in_stream.eof())
  { if(next=='C')
      out_stream<<"C++";
    else 
   out_stream<<next;
    in_stream.get(next);
  }
}




 








 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值