青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

C++研究

C++細節深度探索及軟件工程

  C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
  37 隨筆 :: 0 文章 :: 74 評論 :: 0 Trackbacks



Like C, C++ does not have built-in input/output capability. All C++ compilers, however, come bundled with a systematic, object-oriented I/O package, known as the iostream classes. The stream is the central concept of the iostream classes. You can think of a stream object as a smart file that acts as a source and destination for bytes. A stream's characteristics are determined by its class and by customized insertion and extraction operators.

Through device drivers, the disk operating system deals with the keyboard, screen, printer, and communication ports as extended files. The iostream classes interact with these extended files. Built-in classes support reading from and writing to memory with syntax identical to that for disk I/O, which makes it easy to derive stream classes.


Chang Xinglong , Computer Sci & tec
TianJin university , 300072
TianJin ,China

Msn: cxl82116@msn.com QQ:286259397

OutputStreams

An output stream object is a destination for bytes. The three most important output stream classes are ostream, ofstream, and ostrstream.

The ostream class, through the derived class basic_ostream, supports the predefined stream objects:

  • cout   standard output

  • cerr   standard error with limited buffering

  • clog   similar to cerr but with full buffering

Objects are rarely constructed from ostream; predefined objects are generally used. In some cases, you can reassign predefined objects after program startup. The ostream class, which can be configured for buffered or unbuffered operation, is best suited to sequential text-mode output. All functionality of the base class, ios, is included in ostream. If you construct an object of class ostream, you must specify a streambuf object to the constructor.

The ofstream class supports disk file output. If you need an output-only disk, construct an object of class ofstream. You can specify whether ofstream objects accept binary or text-mode data when constructing the ofstream object or when calling the open member function of the object. Many formatting options and member functions apply to ofstream objects, and all functionality of the base classes ios and ostream is included.

If you specify a filename in the constructor, that file is automatically opened when the object is constructed. Otherwise, you can use the open member function after invoking the default constructor.

Like the run-time function sprintf, the ostrstream class supports output to in-memory strings. To create a string in memory using I/O stream formatting, construct an object of class ostrstream. Because ostrstream objects are write-only, your program must access the resulting string through a pointer to char.

You can construct an output file stream in one of two ways:

Use the default constructor, and then call the open member function.

ofstream myFile; // Static or on the stack
myFile.open( "filename" );

ofstream
* pmyFile = new ofstream; // On the heap
pmyFile->open( "filename" );
 
//Specify a filename and mode flags 
in the constructor call. 

ofstream myFile( 
"filename", ios_base::out);
 


To construct an output string stream, you can use one of two ostrstream constructors. One dynamically allocates its own storage, and the other requires the address and size of a preallocated buffer.

The dynamic constructor is used in the following way:

char* sp;
ostrstream myString;
mystring 
<< "this is a test" << ends;
sp 
= myString.str();  // Get a pointer to the string


The ends "manipulator" adds the necessary terminating null character to the string.

The constructor that requires the preallocated buffer is used in the following way:

char s[32];
ostrstream myString( s, 
sizeof( s ) );
myString 
<< "this is a test" << ends; // Text stored in s


Input Streams :

An input stream object is a source of bytes. The three most important input stream classes are istream, ifstream, and istrstream.

The istream class is best used for sequential text-mode input. You can configure objects of class istream for buffered or unbuffered operation. All functionality of the base class, ios, is included in istream. You will rarely construct objects from class istream. Instead, you will generally use the predefined cin object, which is actually an object of class ostream. In some cases, you can assign cin to other stream objects after program startup.

The ifstream class supports disk file input. If you need an input-only disk file, construct an object of class ifstream. You can specify binary or text-mode data. If you specify a filename in the constructor, the file is automatically opened when the object is constructed. Otherwise, you can use the open function after invoking the default constructor. Many formatting options and member functions apply to ifstream objects. All functionality of the base classes ios and istream is included in ifstream.

Like the library function sscanf, the istrstream class supports input from in-memory strings. To extract data from a character array that has a null terminator, allocate and initialize the string, then construct an object of class istrstream.

If you are using an input file stream (ifstream), you must associate that stream with a specific disk file. You can do this in the constructor, or you can use the open function. In either case, the arguments are the same.

You generally specify an ios_base::openmode flag when you open the file associated with an input stream (the default mode is ios::in). For a list of the open_mode flags, see The open Function. The flags can be combined with the bitwise OR ( | ) operator.

To read a file, first use the fail member function to determine whether it exists:

 

istream ifile( "FILENAME" );
if ( ifile.fail() )
// The file does not exist ...


The Get Function:

The unformatted get member function works like the >> operator with two exceptions. First, the get function includes white-space characters, whereas the extractor excludes white space when the skipws flag is set (the default). Second, the get function is less likely to cause a tied output stream (cout, for example) to be flushed.

A variation of the get function specifies a buffer address and the maximum number of characters to read. This is useful for limiting the number of characters sent to a specific variable, as this example shows:

/ ioo_get_function.cpp
// compile with: /EHsc
// Type up to 24 characters and a terminating character. 
// Any remaining characters can be extracted later.
#include <iostream>
using namespace std;

int main()
{
   
char line[25];
   cout 
<< " Type a line terminated by carriage return\n>";
   cin.
get( line, 25 );
   cout 
<< line << endl;
}

 

Input
  
1234
 

Sample Output

1234
 


The getline Function
The getline member function is similar to the get function. Both functions allow a third argument that specifies the terminating character for input. The default value is the newline character. Both functions reserve one character for the required terminating character. However, get leaves the terminating character in the stream and getline removes the terminating character.

The following example specifies a terminating character for the input stream:

 

// getline_func.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;

int main( )
{
   
char line[100];
   cout 
<< " Type a line terminated by 't'" << endl;
   cin.getline( line, 
100't' );
   cout 
<< line;
}

 

Input
  
test
 

The close Function for Input Streams

The close member function closes the disk file associated with an input file stream and frees the operating system file handle. The ifstream destructor closes the file for you, but you can use the close function if you need to open another file for the same stream object.


[reference:  MSDN & Some other books about C++ Basic]

posted on 2007-06-20 03:09 常興龍 閱讀(6922) 評論(3)  編輯 收藏 引用 所屬分類: STL

評論

# re: C++ streams (How to use ostream & istream ?) 2007-06-21 17:29 pass86
英文比較好啊。  回復  更多評論
  

# re: C++ streams (How to use ostream & istream ?) 2007-09-24 03:54 Gavin
The article is good. But it lacks of some in-depth discussions, such as how buffered/unbuffered stream constructed/used/typical usage, what's the relationship/ownership between stream and streambuf,..., etc.

Futher, I doubt that:
ostrstream myString;
mystring << "this is a test" << ends;
sp = myString.str(); // Get a pointer to the string

The ends "manipulator" adds the necessary terminating null character to the string.

Is above conclusion is correct? and why??  回復  更多評論
  

# re: C++ streams (How to use ostream & istream ?) 2008-07-22 12:12 踏雪赤兔
C++ streams is too slow in my opinion~   回復  更多評論
  

> hi的博客
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            亚洲第一在线综合网站| 欧美久久电影| 欧美日韩精品一本二本三本| 在线激情影院一区| 欧美国产乱视频| 欧美激情小视频| 亚洲欧美不卡| 久久综合狠狠| 亚洲人午夜精品免费| 欧美一级视频精品观看| 亚洲经典自拍| 亚洲精品一区二区三区樱花| 国产欧美日韩精品a在线观看| 欧美韩日精品| 久久深夜福利免费观看| 欧美精品v国产精品v日韩精品| 欧美精品成人91久久久久久久| 久久乐国产精品| 国产精品进线69影院| 国模 一区 二区 三区| 亚洲第一偷拍| 亚洲精品日韩激情在线电影| 久久国产加勒比精品无码| 99视频精品免费观看| 亚洲国产三级网| 亚洲愉拍自拍另类高清精品| 国产午夜精品久久| 99在线热播精品免费99热| 亚洲综合好骚| 欧美三级在线视频| 亚洲国产毛片完整版| 一本色道久久综合狠狠躁篇怎么玩| 99v久久综合狠狠综合久久| 欧美日韩另类字幕中文| 亚洲欧美大片| 欧美国产日本| 久久久久久久一区二区| 欧美色播在线播放| 亚洲国产一区二区在线| 国产日韩欧美视频| 亚洲午夜日本在线观看| 久久久久免费观看| 久久这里只有| 永久域名在线精品| 久久久999精品免费| 牛人盗摄一区二区三区视频| 一区精品在线| 欧美激情一区二区三区四区| 亚洲经典三级| 亚洲欧美精品在线观看| 国产一区二区剧情av在线| 性欧美18~19sex高清播放| 午夜久久久久久| 精品99一区二区| 老牛影视一区二区三区| 久热国产精品视频| 亚洲高清视频在线| 欧美激情视频在线免费观看 欧美视频免费一 | 日韩视频免费大全中文字幕| 久久国产一二区| 亚洲国产精品va在线观看黑人| 中文无字幕一区二区三区| 91久久精品一区| 国产女同一区二区| 欧美久久久久免费| 久久只精品国产| 中国亚洲黄色| 欧美福利视频网站| 久久精品国产免费| 亚洲欧美在线一区二区| 亚洲肉体裸体xxxx137| 国产人久久人人人人爽| 欧美日韩在线高清| 欧美激情精品久久久久久| 欧美在线视频免费| 亚洲男女毛片无遮挡| 99精品国产高清一区二区| 欧美激情一区二区三区在线视频| 久久成人免费| 久久国产一二区| 久久精品在线免费观看| 久久久99免费视频| 久久综合久久综合久久| 久久深夜福利| 免费看成人av| 欧美激情视频给我| 亚洲精品免费在线播放| 亚洲欧洲三级电影| 一区二区三区国产盗摄| 亚洲永久免费视频| 久久精品一区二区三区不卡| 久久精品国产精品亚洲| 久久综合中文色婷婷| 欧美风情在线| 国产精品成人v| 精品不卡一区| 一区二区不卡在线视频 午夜欧美不卡在 | 欧美色综合天天久久综合精品| 欧美激情二区三区| 国产精品专区h在线观看| 精品999日本| 午夜伦欧美伦电影理论片| 欧美中文字幕在线播放| 亚洲高清av| 欧美亚洲日本一区| 欧美成人蜜桃| 国产曰批免费观看久久久| 99天天综合性| 老鸭窝亚洲一区二区三区| 亚洲毛片在线观看.| 久久av一区二区三区亚洲| 欧美日韩亚洲综合一区| 在线观看欧美| 久久久精品久久久久| 亚洲视频一区在线观看| 欧美成年人视频| 在线观看国产精品网站| 久久国产精品久久久久久久久久| 亚洲精品美女91| 欧美日韩精品免费观看| 亚洲精品日韩在线| 亚洲黄色高清| 欧美女激情福利| 亚洲婷婷免费| 在线视频日韩| 国产午夜精品麻豆| 久久综合电影一区| 猛男gaygay欧美视频| 麻豆精品精华液| 亚洲精品日韩欧美| 这里只有精品视频| 国产精品网站在线播放| 久久久午夜精品| 欧美va天堂va视频va在线| 亚洲精品永久免费精品| 一本久久精品一区二区| 国产欧美视频一区二区| 久久久久国产精品一区| 另类av一区二区| 亚洲欧美影院| 欧美96在线丨欧| 欧美一区二区三区免费观看视频| 性欧美暴力猛交另类hd| 亚洲日本电影在线| 久久国产婷婷国产香蕉| aⅴ色国产欧美| 久久精品日韩欧美| 这里是久久伊人| 噜噜噜噜噜久久久久久91| 羞羞答答国产精品www一本| 久久国产精品一区二区| 亚洲天堂av高清| 欧美成人午夜| 免费观看成人| 国内激情久久| 亚洲欧美日韩另类精品一区二区三区 | 欧美高清视频| 久久精品国产亚洲一区二区三区| 欧美日韩精品免费观看视频完整 | 亚洲一区二区三区777| 亚洲第一精品久久忘忧草社区| 中国成人亚色综合网站| 一区二区免费看| 欧美日韩国产不卡在线看| 亚洲国产精品一区二区第一页 | 欧美一区二区精美| 欧美日韩情趣电影| 亚洲人成高清| 亚洲伦理一区| 欧美日韩免费观看一区三区 | 日韩视频精品在线| 亚洲精品视频在线| 欧美日本精品| 午夜精品成人在线视频| 亚洲欧美日韩天堂| 国产曰批免费观看久久久| 久久久久久国产精品mv| 欧美激情第六页| 国产精品99久久久久久久女警| 欧美精选午夜久久久乱码6080| 一本色道久久综合亚洲精品不卡 | 欧美一区二区三区视频在线 | 夜久久久久久| 久久久999成人| 亚洲欧洲精品一区| 国产精品一区二区欧美| 欧美影院成人| 一本色道久久综合| 狂野欧美激情性xxxx欧美| 亚洲激情视频网站| 欧美色网在线| 免费成人你懂的| 久久蜜桃av一区精品变态类天堂| 亚洲国产电影| 老司机精品视频一区二区三区| 亚洲欧美国产精品va在线观看 | 欧美一区=区| 亚洲网站视频| 一区二区三区四区国产精品| 欧美成熟视频|