C++µÄÄÇЩÊ£ºÁ÷ÓëIOÀà(Îå)

2014-11-24 12:41:39 ¡¤ ×÷Õß: ¡¤ ä¯ÀÀ: 2
²»ÔÙÏÔʾÕûÐÍÖµµÄ½øÖÆ¡£ÓÐʱºòÎÒÃÇÐèÒª½«16½øÖÆÊä³öΪ´óдÈç0X FF£¬¿ÉÒÔÓòÙ×Ý·ûuppercaseºÍnouppercaseÀ´¿ØÖÆÁ÷Êä³öµÄ´óСд״̬¡£
cout << uppercase << showbase << hex << 20 << 1024
<< nouppercase << noshowbase << dec << endl;
8.4 ¿ØÖƸ¡µãÊý¸ñʽ
´òÓ¡¾«¶ÈÊÇͨ¹ýprecision³ÉÔ±»òʹÓÃsetprecision²Ù×Ý·ûÀ´¸Ä±ä¡£ÆäÖÐprecisionÊÇÒ»¸öÖØÔØº¯Êý£¬Ò»¸ö°æ±¾½ÓÊÜint²ÎÊý£¬½«¾«¶ÈÉèÖÃΪ´ËÖµ£¬²¢·µ»Ø¾É¾«¶ÈÖµ¡£ÁíÍâÒ»¸ö°æ±¾²»½ÓÊܲÎÊý£¬·µ»Øµ±Ç°¾«¶ÈÖµ¡£setprecision²Ù×Ý·û½ÓÊÜÒ»¸ö²ÎÊý£¬ÓÃÀ´ÉèÖþ«¶È¡£
ÓÃscientificÓÃÀ´Ö¸¶¨¿ÆÑ§¼ÇÊý·¨£¬fixedÖ¸¶¨Îª¶¨µãÊ®½øÖÆ£¬hexfloatÖ¸¶¨ÎªÊ®Áù½øÖƵĸ¡µãÊý¡£defaultfloat½«Á÷»Ö¸´µ½Ä¬ÈϵÄ״̬¡£
ÉèÖÃshowpoint¿ÉÒÔÓÃÀ´Ç¿ÖÆ´òӡСÊý¡£
8.5 Êä³ö²¹°×
setw£ºÖ¸¶¨ÏÂÒ»¸öÊý×Ö»ò×Ö·û´®µÄ×îС¿Õ¼ä
left£º±íʾ×ó¶ÔÆëÊä³ö¡£
right£º±íʾÓÒ¶ÔÆëÊä³ö£¬ÓÒ¶ÔÆëÊÇĬÈϸñʽ¡£
internal£º¿ØÖƸºÊýµÄ·ûºÅµÄλÖã¬Ëü×ó¶ÔÆë·ûºÅ£¬ÓÒ¶ÔÆëÖµ£¬ÓÿոñÌîÂúËùÓÐÖмä¿Õ¼ä¡£
setfill£ºÔÊÐíÖ¸¶¨Ò»¸ö×Ö·û´úÌæÄ¬ÈϵĿոñÀ´²¹°×Êä³ö¡£
¸´ÖÆ´úÂë
1 int i = -16;
2 double d = 3.14159;
3 cout << "i:" << setw(12) << i << '\n'
4 << "d:" << setw(12) << d << '\n';
5 cout << left
6 << "i:" << setw(12) << i << '\n'
7 << "d:" << setw(12) << d << '\n';
8 cout << right
9 << "i:" << setw(12) << i << '\n'
10 << "d:" << setw(12) << d << '\n';
11 cout << internal
12 << "i:" << setw(12) << i << '\n'
13 << "d:" << setw(12) << d << '\n';
14 cout << setfill('#')
15 << "i:" << setw(12) << i << '\n'
16 << "d:" << setw(12) << d << '\n'
17 << setfill(' ');
¸´ÖÆ´úÂë
image
9£¬Á÷µÄËæ»ú·ÃÎÊ
²»Í¬µÄÁ÷ÀàÐÍÒ»°ãÖ§³Ö¶ÔÏà¹ØÁ÷ÖÐÊý¾ÝµÄËæ»ú·ÃÎÊ¡£¿ÉÒÔÖØÐ¶¨Î»Á÷£¬ÒÔ±ã»·ÈÆÌø¹ý£¬Ê×ÏȶÁ×îºóÒ»ÐУ¬ÔÙ¶ÁµÚÒ»ÐУ¬ÒÔ´ËÀàÍÆ¡£±ê×¼¿âÌṩһ¶Ôº¯ÊýÀ´¶¨Î»£¨seek£©¸ø¶¨Î»Öò¢¸æËߣ¨tell£©Ïà¹ØÁ÷Öеĵ±Ç°Î»Öá£
9.1 seekºÍtellº¯Êý
seekg£ºÖØÐ¶¨Î»ÊäÈëÁ÷Öеıê¼Ç
tellg£º·µ»ØÊäÈëÁ÷Öбê¼ÇµÄµ±Ç°Î»ÖÃ
seekp£ºÖØÐ¶¨Î»Êä³öÁ÷Öеıê¼Ç
tellp£º·µ»ØÊä³öÁ÷Öбê¼ÇµÄµ±Ç°Î»ÖÃ
Âß¼­ÉÏ£¬Ö»ÄÜÔÚistream»òÕßifstream»òÕßistringstreamÉÏʹÓÃg°æ±¾£¬²¢ÇÒÖ»ÄÜÔÚostreamÀàÐÍ»òÆäÅÉÉúÀàÐÔofstream»òÕßostringstreamÖ®ÉÏʹÓÃp°æ±¾¡£iostream¶ÔÏó£¬fstream»òÕßstringstream¶ÔÏó¶ÔÏà¹ØÁ÷¼È¿ÉÒÔ¶ÁÒ²¿ÉÒÔд£¬¿ÉÒÔʹÓÃÁ½¸ö°æ±¾µÄÈÎÒâ°æ±¾¡£9.
9.2 Ö»ÓÐÒ»¸ö±ê¼Ç
ËäÈ»±ê×¼¿âÇø·ÖÊäÈëºÍÊäÈë¶øÓÐÁ½¸ö°æ±¾£¬µ«ËüÖ»ÔÚÎļþÖÐά³ÖÒ»¸ö±ê¼Ç¡ª¡ªÃ»ÓпÉÇø·ÖµÄ¶Á±ê¼ÇºÍд±ê¼Ç¡£
Ö»ÊÇÊÔͼÔÚifstream¶ÔÏóÉϵ÷ÓÃtellpµÄʱºò£¬±àÒëÆ÷½«»á¸ø³ö´íÎóÌáʾ¡£·´Ö®ÒàÈ»¡£
ʹÓüȿÉÒÔ¶ÁÓÖÄÜдµÄfstreamÀàÐÍÒÔ¼°stringstreamÀàÐ͵Äʱºò£¬Ö»ÓÐÒ»¸ö±£´æÊý¾ÝµÄ»º³åÇøºÍÒ»¸ö±íʾ»º³åÆ÷Öе±Ç°Î»Öõıê¼Ç£¬±ê×¼¿â½«g°æ±¾ºÍp°æ±¾¶¼Ó³Éäµ½Õâ¸ö±ê¼Ç¡£
9.3 ÆÕͨiostream¶ÔÏóÒ»°ã²»ÔÊÐíËæ»ú·ÃÎÊ¡£
9.4 ÖØÐ¶¨Î»±ê¼Ç
¸´ÖÆ´úÂë
seekg£¨new_position£©;
seekp (new_position);
seekg( offset, dir);
seekp( offset, dir);
¸´ÖÆ´úÂë
µÚÒ»¸ö°æ±¾½«µ±Ç°Î»ÖÃÇл»µ½¸ø¶¨µØµã£¬µÚ¶þ¸ö°æ±¾½ÓÊÜÒ»¸öÆ«ÒÆÁ¿ÒÔ¼°´ÓºÎ´¦¼ÆËãÆ«ÒÆµÄָʾÆ÷¡£
9.5 ·ÃÎʱê¼Ç
tellº¯Êý·µ»ØµÄÒ»¸öÖµ£¬Ê¹ÓÃÊʵ±ÀàµÄpos_type³ÉÔ±À´±£´æ¡£
10£¬Ò»¸öʵÀý
¼Ù¶¨¸ø¶¨Ò»¸öÎļþÀ´¶Á£¬ÎÒÃǽ«ÔÚÎļþµÄĩβдһ¸öÐÂÐУ¬¸ÄÐаüº¬ÁËÿһÐпªÍ·µÄÏà¶ÔλÖ㨳ÌÐò²»±ØÐ´µÚÒ»ÐÐµÄÆ«ÒÆÁ¿£¬ÒòΪËü×ÜÊÇ0£©¡£ÀýÈç¸ø¶¨ÏÂÃæµÄÎļþ£¬
abcd
efg
hi
j
Õâ¶Î³ÌÐòÓ¦²úÉúÐ޸ĹýµÄÎļþÈçÏ£º
abcd
efg
hi
j
5 9 12 14
¸´ÖÆ´úÂë
1 #include
2 #include
3 #include
4
5 using std::fstream;
6 using std::cerr;
7 using std::endl;
8 using std::ifstream;
9 using std::ofstream;
10 using std::string;
11 using std::getline;
12 using std::cout;
13 //using namespace std;
14
15 int main()
16 {
17 fstream inOut("copyOut.txt",
18 fstream::ate | fstream::in | fstream::out); //ÓÃate·½Ê½´ò¿ª£¬»á½«ÎļþµÄλÖö¨Î»µ½Îļþĩβ¡£
19
20 if( !inOut ) {
21 cerr << "unable to open file" <
22 return EXIT_FAILURE;
23 }
24
25 inOut.seekg(-1,fstream::end); //go to the last char
26 if( inOut.peek() != 10) //if the last char of the file is not a newline,add it.
27 {
28 inOut.seekg(0,fstream::end);
29 inOut.put('\n');
30 }
31
32 inOut.seekg(0,fstream::end);
33 ifstream::pos_type endMark = inOut.tellg(); //record the last position .
34
35 inOut.seekg(0,fstream::beg);
36
37 int cnt = 0; //accumulator for byte count
38 string line; //hold each line of input
39
40 while( inOut && inOut.tellg() != endMark
41 && getline(inOut , line)
42 )
43 {
44 cnt += line.size() + 1; // add 1 to acount for the newline
45
46 ifstream::pos_type mark = inOut.tellg();
47 inOut.seekp( 0, fstream::end); //set write marker to end
48 inOut