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

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 常興龍 閱讀(6928) 評論(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>
            久久久久久久成人| 欧美丰满高潮xxxx喷水动漫| 亚洲精品自在久久| 亚洲成色最大综合在线| 影院欧美亚洲| 亚洲第一精品影视| 亚洲每日更新| 午夜视频一区在线观看| 久久精品综合网| 欧美在线一二三四区| 六月丁香综合| 国产精品捆绑调教| 伊人精品久久久久7777| 欧美激情精品久久久久久久变态| 亚洲国产精品激情在线观看| 一区二区高清| 久久久蜜桃一区二区人| 国产精品久久九九| 亚洲精品一区二区三区樱花| 久久激情五月丁香伊人| 日韩一级成人av| 暖暖成人免费视频| 黑丝一区二区| 亚洲一区影院| 亚洲国产你懂的| 欧美视频精品在线| 亚洲国产精品精华液2区45| 午夜久久资源| 久久免费偷拍视频| 国产亚洲激情| 亚洲一区高清| 久久九九免费| 影音先锋中文字幕一区| 日韩视频中文字幕| 国产一区二区高清不卡| 最新日韩在线视频| 欧美激情精品久久久六区热门| 亚洲影院一区| 麻豆乱码国产一区二区三区| 午夜精品福利电影| 免费日韩av片| 久久久精品一区| 国产精品国产三级国产aⅴ入口| 久久精品午夜| 欧美性久久久| 午夜亚洲性色福利视频| 亚洲国产精品一区二区www在线| 国产精品激情电影| 亚洲国产欧美一区二区三区久久 | 久久国产色av| 欧美日韩1区| 99成人精品| 亚洲人成在线影院| 欧美日韩精品一区二区天天拍小说 | 在线观看一区二区精品视频| 亚洲视频第一页| 国产精品欧美在线| 欧美在线日韩精品| 久久精品国语| 欧美一区二区视频观看视频| 久久精品国产精品 | 欧美激情精品久久久久久久变态| 久久久精品日韩欧美| 国产精品激情电影| 99精品黄色片免费大全| 国产精品久久网站| 亚洲毛片播放| 一区二区三区日韩精品| 亚洲一区中文| 韩国久久久久| 久久国产精品网站| 久久久噜噜噜久久狠狠50岁| 欧美 日韩 国产精品免费观看| 久久久久综合网| 国际精品欧美精品| 日韩视频免费在线观看| 99精品欧美一区二区三区| 欧美精品免费看| 久久高清一区| 狠狠做深爱婷婷久久综合一区| 欧美在线播放| 麻豆精品在线视频| 欧美日韩国产一级| 亚洲精选大片| 亚洲欧美高清| 欧美激情一区二区三区高清视频 | 亚洲男女自偷自拍| 国产精品成人免费视频| 亚洲欧美日韩在线高清直播| 影音先锋日韩有码| 欧美不卡一区| 老司机精品视频网站| 最新成人在线| 久久久国产精品一区二区中文| 欧美69wwwcom| 国产九九精品视频| 亚洲人成在线影院| 亚洲一区二区三区在线| 欧美二区乱c少妇| 日韩视频在线免费| 久久精品国产久精国产一老狼| 狠狠爱综合网| 欧美精品videossex性护士| 中日韩美女免费视频网址在线观看| 欧美在线免费一级片| 亚洲精品久久嫩草网站秘色| 欧美午夜精品久久久| 久久精品一区蜜桃臀影院| 亚洲国产精品va在线看黑人| 午夜精品久久久久久久99樱桃| 激情欧美国产欧美| 久久国产精品一区二区| 亚洲精品永久免费精品| 久久免费的精品国产v∧| 日韩亚洲欧美成人| 国产亚洲精品aa午夜观看| 欧美国产三区| 久久精品麻豆| 亚洲性视频h| 欧美在线观看视频在线| 亚洲精品综合| 国产一区三区三区| 欧美色区777第一页| 久久综合久久88| 欧美韩日精品| 欧美伊人精品成人久久综合97| 亚洲三级性片| 在线日韩欧美| 国产一区二区三区免费不卡| 欧美日韩在线一区二区| 欧美成人免费网| 久久精品视频免费观看| 亚洲永久在线| 亚洲美女在线一区| 亚洲高清视频在线观看| 亚洲视频久久| 国产日韩在线亚洲字幕中文| 久久大逼视频| 亚洲综合首页| 欧美成人高清| 老司机免费视频一区二区| 午夜在线精品| 午夜精品久久久久久久白皮肤| 在线中文字幕不卡| 日韩一级成人av| 最新69国产成人精品视频免费| 一区二区在线视频观看| 国内久久精品| 国内外成人在线| 国产日韩欧美在线| 国产婷婷97碰碰久久人人蜜臀| 国产精品欧美经典| 国产精品自拍视频| 国产日韩精品在线| 国产主播一区| 亚洲第一二三四五区| 亚洲国产精品成人精品| 亚洲精品国产精品国产自| 亚洲精品一区二| 99国产精品国产精品久久 | 欧美视频一区二区三区…| 欧美日韩成人在线视频| 欧美日韩一区国产| 久久久久久久97| 久久一区二区精品| 欧美激情成人在线| 欧美日韩一区二区在线视频| 国产精品美女久久久久久久| 国产深夜精品福利| 在线日本成人| 一本大道久久a久久综合婷婷| 国产一区二区三区久久久久久久久| 国产色爱av资源综合区| 原创国产精品91| 99视频超级精品| 午夜欧美大片免费观看| 久久性天堂网| 久久国产精彩视频| 免费国产一区二区| 久久精品国产99精品国产亚洲性色| 久久综合狠狠| 亚洲精品中文字幕有码专区| 亚洲在线观看视频| 久久综合色天天久久综合图片| 欧美另类变人与禽xxxxx| 免费看亚洲片| 欧美吻胸吃奶大尺度电影| 国产专区综合网| 亚洲最新在线| 久久夜色精品国产亚洲aⅴ | 亚洲在线中文字幕| 免费在线观看一区二区| 在线亚洲欧美视频| 久久综合五月| 国产麻豆日韩| a91a精品视频在线观看| 久久青草久久| 中文国产成人精品| 欧美国产丝袜视频| 黑人极品videos精品欧美裸|