码迷,mamicode.com
首页 > 其他好文 > 详细

fstream函数

时间:2017-11-03 23:56:47      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:first   ati   turn   get   define   returns   bsp   c_str   file   

<fstream>

Defines types ifstream and ofstream representing input and output files respectively. ifstream is derived from istream, inheriting all its operations (such as >>). In addition,

  ifstream in(cp);          // Open file named cp for reading
  ifstream in(cp, ios::in | ios::binary);  // Open in binary mode
  bool(in);                 // true if open successful

cp is the file name. It must be a char*, not string (use s.c_str() to convert string s). Input is normally in text mode. In Windows, carriage returns (‘\r‘) are discarded, and an ASCII 26 (‘\032‘) signals end of file. In binary mode and in UNIX, no such translation occurs. The file is closed when the ifstream is destroyed.

  {
    ifstream f("input.dat", ios::in | ios::binary);
    if (!f)
      cerr << "File not found\n";
    else {
      int i=f.get();  // First byte or EOF if empty
    }
  } // f closed here

ofstream is derived from ostream, inheriting all its operations (such as <<). In addition,

  ofstream os(cp);          // Open file named cp for writing
  ofstream os(cp, ios::out | ios::binary);  // Open in binary mode

In text mode in Windows, writing ‘\n‘ actually writes "\r\n". The file named cp is overwritten if it exists, or created otherwise. The file is flushed and closed when the ofstream is destroyed.

 

fstream函数

标签:first   ati   turn   get   define   returns   bsp   c_str   file   

原文地址:http://www.cnblogs.com/enyala/p/7780513.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!