Chinaunix首页 | 论坛 | 博客
  • 博客访问: 359784
  • 博文数量: 100
  • 博客积分: 2500
  • 博客等级: 大尉
  • 技术积分: 1209
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-15 21:24
文章分类

全部博文(100)

文章存档

2011年(100)

分类: C/C++

2011-04-19 14:39:19

  • ofstream: Stream class to write on files
  • ifstream: Stream class to read from files
  • fstream: Stream class to both read and write from/to files.
Open:
  1. open (filename, mode);
mode:




Close:
  1. myfile.close();

  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;

  5. int
  6. main(void)
  7. {
  8.         ofstream myfile("example.txt");
  9.         if (myfile.is_open()) {
  10.                 myfile << "This is a line." << endl;
  11.                 myfile << "This is another line." << endl;
  12.                 myfile.close();
  13.         } else {
  14.                 cout << "Unable to open file" << endl;
  15.         }

  16.         string line;
  17.         ifstream myfile2("example.txt");
  18.         if (myfile2.is_open()) {
  19.                 while (myfile2.good()) {
  20.                         getline(myfile2, line);
  21.                         cout << line << endl;
  22.                 }
  23.                 myfile2.close();
  24.         } else {
  25.                 cout << "Unable to open file: 2" << endl;
  26.         }

  27.         return (0);
  28. }
state flag:
bad()Returns true if a reading or writing operation fails. For example in the case that we try to write to a file that is not open for writing or if the device where we try to write has no space left.fail()Returns true in the same cases as bad(), but also in the case that a format error happens, like when an alphabetical character is extracted when we are trying to read an integer number.eof()Returns true if a file open for reading has reached the end.good()It is the most generic state flag: it returns false in the same cases in which calling any of the previous functions would return true.
tellg() tellp()
These two member functions have no parameters and return a value of the member type pos_type, which is an integer data type representing the current position of the get stream pointer (in the case of tellg) or the put stream pointer (in the case of tellp).

seekg() seekp()
These functions allow us to change the position of the get and put stream pointers. Both functions are overloaded with two different prototypes.
  1. seekg ( position );
  2. seekp ( position );

  1. seekg ( offset, direction );
  2. seekp ( offset, direction );
direction:


Binary file:
File streams include two member functions specifically designed to input and output binary data sequentially: write and read. The first one (write) is a member function of ostream inherited by ofstream. And read is a member function of istream that is inherited by ifstream. Objects of class fstream have both members.
  1. write ( memory_block, size );
  2. read ( memory_block, size );
Buffer and synchronization:
When we operate with file streams, these are associated to an internal buffer of type streambuf. This buffer is a memory block that acts as an intermediary between the stream and the physical file.
When the buffer is flushed, all the data contained in it is written to the physical medium (if it is an output stream) or simply freed (if it is an input stream). This process is called synchronization and takes place under any of the following circumstances:
  • When the file is closed: before closing a file all buffers that have not yet been flushed are synchronized and all pending data is written or read to the physical medium.
  • When the buffer is full: Buffers have a certain size. When the buffer is full it is automatically synchronized.
  • Explicitly, with manipulators: When certain manipulators are used on streams, an explicit synchronization takes place. These manipulators are: flush and endl.
  • Explicitly, with member function sync(): Calling stream's member function sync(), which takes no parameters, causes an immediate synchronization. This function returns an int value equal to -1 if the stream has no associated buffer or in case of failure. Otherwise (if the stream buffer was successfully synchronized) it returns 0.

阅读(1837) | 评论(0) | 转发(0) |
0

上一篇:预处理

下一篇:String I

给主人留下些什么吧!~~