• <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>
            posts - 29,comments - 10,trackbacks - 0
            1、 格式化輸出:

            1) width()函數:可以為接下來的所有顯示要素指定默認的寬度。

            2) setw()函數:設置數據項實用的寬度

            3) fill()函數:當輸入寬度非默認寬度時可以設置填充字符的值

            4) setiosflags(ios::left)函數:表示輸出值的對其方式

            5) dec、hexoct本別表示十進制、十六進制和八進制

            6) put:把單個字符寫入輸入流中

            7) get:類似于提取運算符>>,知識空字符被包含到輸入中。

            #include <iostream>

            #include 
            <iomanip>

            using namespace std;
            int main()

            {

                   cout.width(
            10);

                   cout
            <<setiosflags(ios::left)<<oct<<25<<endl;

                   cout.width(
            10);

                   cout
            <<setiosflags(ios::right)<<hex<<25<<endl;

                   cout
            <<dec<<setfill('b')<<setw(10);

                   cout
            <<1024<<"OK"<<endl;

                   cout.put(
            'H'); 

                
            return 0;

            }

            #include <iostream>

            int main()
            {
                
            char line[25], ch = 0*cp;

                std::cout 
            << " Type a line terminated by 'x'"
                          
            << std::endl << '>';
                cp 
            = line;
                
            while (ch != 'x')
                {
                    std::cin 
            >> ch;
                    
            *cp++ = ch;
                }
                
            *cp = '\0';
                std::cout 
            << ' ' << line;

                std::cout 
            << "\n Type another one" << std::endl << '>';
                cp 
            = line;
                ch 
            = 0;
                
            while (ch != 'x')
                {
                    std::cin.
            get(ch);
                    
            *cp++ = ch;
                }
                
            *cp = '\0';
                std::cout 
            << ' ' << line;
                
                
            return 0;
            }

            2、 文件輸入/輸出

            1)ofstream類:文件輸出

            #include <fstream>
            #include 
            <iostream>

            int main()
            {
                std::cout 
            << "Opening file" << std::endl;
                std::ofstream tfile(
            "test.dat", std::ios::app);

                std::cout 
            << "Writing to file" << std::endl;
                tfile 
            << ", and these are more";

                
            return 0;
            }
            標志 描述
            app 把所有的新數據寫到文件尾部
            ate 把所有的新數據寫到文件尾部,如果程序移動了文件指針,就把數據寫到當前位置
            binary 以二進制模式而不是文本模式打開文件
            out    打開由于輸出的文件,刪除文件的當前內容。該模式只在沒有指定文件打開模式時使用
            trunc  打開用于輸出的文件,刪除文件的當前內容
                  相關函數:
            函數 描述
            attach() 把打開的文件與流關聯
            close() 在刷新未保存的數據后關閉文件
            flush() 刷新流
            open() 打開一個文件并把它與流關聯
            put() 向流中寫入一個字符
            rdbuf() 返回與流關聯的filebuf對象
            seekp() 設置流文件指針的位置
            setmode() 把流設置成二進制模式或文本模式
            tellp() 獲取流文件指針的位置
            write() 向流中寫入一組字節

            #include <iostream>
            #include 
            <string>
            #include 
            <fstream>


            int main() 
            {     
                std::
            string str("This is a test");
                
                
            // Create an output stream object.
                std::ofstream tfile;
                
                
            // Associate a file with the stream.
                tfile.open("testfile.txt");
                
                
            // Write a string one character at a time.
                for (int x=0; x<14++x)
                
            {
                    std::cout 
            << "File pointer: " << tfile.tellp();
                    tfile.put(str[x]);
                    std::cout 
            << "  " << str[x] << std::endl;
                }

                
                
            return 0;
            }


            2)ifstream類:輸出類
                  相關函數
            eof() 測試文件是否結束
            seekg() 在文件中定位,有beg、cur和end表示起始位置,seekg(5,ios::cur)
            tellg() 指示在文件中的位置
            #include <fstream>
            #include 
            <iostream>

            int main()
            {
                std::ifstream tfile(
            "test.dat");
                tfile.seekg(
            6);        // Seek six characters in

                
            while (!tfile.eof())
                {
                    
            char ch;
                    tfile.
            get(ch);
                    
            if (!tfile.eof())
                        std::cout 
            << ch;
                }

                
            return 0;
            }
            3)文件的輸入輸出
            #include <fstream>
            #include 
            <cctype>
            #include 
            <iostream>

            int main()
            {
                
            char* fname = "test.dat";

                
            // Read the file into an array.
                std::ifstream tfile(fname, std::ios::in |
                    std::ios::
            out | std::ios::binary);
                std::ostream ofile(tfile.rdbuf());
                
            char tdata[100];
                
            int i = 0;
                
            while (!tfile.eof() && i < sizeof tdata)
                    tfile.
            get(tdata[i++]);

                
            // Write the array to the file.
                ofile.seekp(0, std::ios::end);
                ofile 
            << "\r\n";
                
            for (int j = 0; j < i-1; j++)
                    ofile.put(static_cast
            <char>(toupper(tdata[j])));
                
                
            return 0;
            }
            posted on 2009-06-22 09:16 The_Moment 閱讀(566) 評論(0)  編輯 收藏 引用 所屬分類: C\C++
            久久精品aⅴ无码中文字字幕不卡| 久久精品国产亚洲av高清漫画 | 久久精品国产亚洲AV不卡| 热久久国产欧美一区二区精品| 久久久这里有精品中文字幕| 99久久精品免费看国产一区二区三区| 亚洲精品无码久久久久| 久久精品国产只有精品2020| 久久久久99精品成人片| 久久无码AV一区二区三区| 国内精品久久久久影院优| 精品国产乱码久久久久久浪潮| 亚洲女久久久噜噜噜熟女| 亚洲成人精品久久| 久久不见久久见免费视频7| 欧美午夜精品久久久久久浪潮| 久久精品人人槡人妻人人玩AV | 狠狠狠色丁香婷婷综合久久五月| 激情综合色综合久久综合| 久久精品九九亚洲精品| 国产精品久久久久免费a∨| 国产福利电影一区二区三区久久老子无码午夜伦不 | 三上悠亚久久精品| 综合久久一区二区三区 | 久久se这里只有精品| 久久66热人妻偷产精品9| 日本五月天婷久久网站| 国产精品欧美久久久久天天影视| 久久99精品久久久久久动态图 | 久久er国产精品免费观看2| 久久久久久亚洲AV无码专区| 久久综合亚洲色HEZYO社区| 欧美精品一区二区久久| 久久男人中文字幕资源站| 久久99精品国产麻豆婷婷| 国产午夜精品久久久久九九| 九九久久精品国产| 久久天天躁狠狠躁夜夜不卡 | 欧美va久久久噜噜噜久久| 久久99亚洲网美利坚合众国| 乱亲女H秽乱长久久久|