• <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 - 15,  comments - 0,  trackbacks - 0
            對(duì)于文件輸入,C++使用類似于cout的東西。下面來復(fù)習(xí)一些有關(guān)將cout用于控制臺(tái)輸出的基本事實(shí),為文件輸入做準(zhǔn)備:
                ● 必須包含頭文件iostream。
                ● 頭文件iostream定義了一個(gè)用處理輸出的ostream類。
                ● 頭文件iostream聲明了一個(gè)名為cout的ostream變量(對(duì)象)。
                ● 必須指明名稱空間std:例如,為引用元素cout和endl,必須使用編譯指令using或前綴std::。
                ● 可以結(jié)合使用cout和操作符“>>”來顯示各種類型的數(shù)據(jù)。

                文件輸出與此極其相似:
                ● 必須包含頭文件fstream。
                ● 需要聲明一個(gè)或多個(gè)ofstream變量(對(duì)象),并以自己喜歡的方式對(duì)其進(jìn)行命名,條件是遵守常用的命名規(guī)則。
                ● 必須指明名稱空間std:例如,為引用元素ofstream,必須使用編譯指令using或前綴std::。
                ● 需要將ofstream對(duì)象與文件關(guān)聯(lián)起來。為此,方法之一是使用open()方法。
                ● 使用完文件后,應(yīng)使用close()方法將其關(guān)閉。
                ● 可結(jié)合使用ofstream對(duì)象和操作符>>來輸出各種類型的數(shù)據(jù)。
               
                注意,雖然頭文件iostream提供了一個(gè)預(yù)先定義好的名為cout的ostream對(duì)象,但您必須聲明自己的ofstream對(duì)象,為其命名,并將其同文件關(guān)聯(lián)起來。下面演示了如何聲明這種對(duì)象。
               
                    ofstream outFile;   //outFile an ofstream object
                    ofstream fount;    //fout an ofstream object

                下面演示了如何將這種對(duì)象與特定的文件關(guān)聯(lián)起來:

                    outFile.open("fish.txt"); //outFile used to write to the fish.txt file
                    char filename[50];
                    cin >> filename;   //user specifies a name
                    fout.open(filename);   //fout used to read specified file

                注意,方法open()接受一個(gè)C-風(fēng)格字符串作為參數(shù),這可以是一個(gè)字面字符串,也可以是存儲(chǔ)在數(shù)組中的字符串。
                下面演示了如何使用這種對(duì)象:

                    double wt = 125.8;
                    outFile << wt;    //write a number to fish.txt
                    char line[81] = "Objects are closer than they appear.";
                    fin << line << endl;   //write a line of text

                重要的是,聲明一個(gè)ofstream對(duì)象并將其同文件關(guān)聯(lián)起來后,便可以像使用cout那樣使用它。所有可用于cout的操作和方法(如<<、enld和setf())都可用于ofstream對(duì)象(如前述范例中的outFile和fout)。

                總之,使用文件輸出的主要步驟如下:
                1. 包含頭文件fstream。
                2. 創(chuàng)建一個(gè)ofstream對(duì)象。
                3. 將該ofstream對(duì)象同一個(gè)文件關(guān)聯(lián)起來。
                4. 就像使用count那樣使用該ofstream對(duì)象。
                程序清單6.15中的程序演示了這種方法。它要求用戶輸入信息,然后將信息顯示到屏幕上,再將這些信息寫入到文件中。讀者可以使用文本編輯器來查看該輸出文件的內(nèi)容。

                程序清單6.15 outfile.cpp
                //outfile.cpp -- writing to a file
                #include <iostream>
                #include <fstream>

                int main() {
                    using namespace std;
                    char automobile[50];
                    int year;
                    double a_price;
                    double d_price;
                    ofstream outFile;   //create object for output
                    outFile.open("carinfo.txt"); //associate with a file

                    cout << "Enter the make and model of automobile:";
                    cin.getline(automobile, 50);
                    cout << "Enter the model year:";
                    cin >> year;
                    cout << "Enter the original asking price:";
                    cin >> a_price;
                    d_price = 0.913 * a_price;

                    // display information on screen with count

                    cout << fixed;
                    cout.precision(2);
                    cout.setf(ios_base::showpoint);
                    cout << "Make and model:" << automobile << endl;
                    cout << "Year:" << year << endl;
                    cout << "Was asking $" << a_price << endl;
                    cout << "Now asking $" << d_price << endl;

                    // now do exact same things using outFile instead of cout

                    outFile << fixed;//如果輸出的數(shù)據(jù)是浮點(diǎn)數(shù).則按定點(diǎn)表示法輸出
                    outFile.precision(2);//按照指定的精度控制浮點(diǎn)數(shù)輸出
                    outFile.setf(ios_base::showpoint);//為輸出的數(shù)據(jù)強(qiáng)制加小數(shù)點(diǎn)以及小數(shù)點(diǎn)后
                    outFile << "Make and model:" << automobile << endl;
                    outFile << "Year:" << year << endl;
                    outFile << "Was asking $" << a_price << endl;
                    outFile << "Now asking $" << d_price << endl;

                    outFile.close(); //done with file
                    return 0;
                }  

                該程序的最后一部分與cout部分相同,只是將cout替換為outFile而已。下面是該程序的運(yùn)行情況:
                Enter the make and model of automobile: Flitz Pinata
                Enter the model year: 2001
                Enter the original asking price :28576
                Make and model: Flitz Pinata
                Year: 2001
                Was asking $28576.00
                Now asking $26089.89
               
                屏幕輸出的是使用cout的結(jié)果。如果您查看該程序的可執(zhí)行文件所在的目錄,將看到一個(gè)名為carinfo.txt的新文件,其中包含使用outFile生成的輸出。
               
                程序說明
                在程序清單6.15的程序中,聲明一個(gè)ofstream對(duì)象后,便可以使用方法open()來將該對(duì)象同一個(gè)特定的文件關(guān)聯(lián)起來:

                    ofstream outFile;   //create object for output
                    outFile.open("carinfo.txt"); //associate with a file

                程序使用完該文件后,應(yīng)該將其關(guān)閉:

                    outFile.close();

                注意,方法close()不需要使用文件名作為參數(shù),這是因?yàn)閛utFile已經(jīng)同特定的文件關(guān)聯(lián)起來了。如果您忘記關(guān)閉文件,程序正常終止時(shí)將自動(dòng)關(guān)閉它。
                outFile可使用cout可使用的任何方法。它不但能夠使用操作符<<,還可以使用各種格式化方法,如setf()和precision()。這些方法只影響調(diào)用它們的對(duì)象。例如,對(duì)于不同的對(duì)象,可以提供不同的值:

                    cout.precision(2); //use a precision of 2 for the display
                    outFile.porecision(4); //use a precision of 4 for file output

                讀者需要記住的重點(diǎn)是,創(chuàng)建好ofstream對(duì)象(如outFile)后,便可以像使用cout那樣使用它。
                回到open()方法:

                    outFile.open("carinfo.txt");

                在這里,該程序運(yùn)行之前,文件carinfo.txt并不存在。在這種情況下,方法open()將新建一個(gè)名為carinfo.txt的文件。如果在此運(yùn)行該程序,文件carinfo.txt將存在,此時(shí)情況將如何呢?默認(rèn)情況下,open()將首先截?cái)嘣撐募磳⑵溟L度截短到零——丟其原有的內(nèi)容,然后將新的輸出加入到該文件中。第17章將介紹如何修改這種默認(rèn)行為。

                    警告:打開已有的文件,以接受輸出時(shí),默認(rèn)將它其長度截短為零,因此原來的內(nèi)容將丟失。

                打開文件用于接受輸入時(shí)可能失敗。例如,指定的文件可能已存在,但禁止對(duì)其進(jìn)行訪問。因此細(xì)心的程序員將檢查打開文件的操作是否成功,這將在下一個(gè)例子中介紹。

            posted on 2010-10-07 19:10 王秋林 閱讀(1402) 評(píng)論(0)  編輯 收藏 引用

            只有注冊用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            <2010年10月>
            262728293012
            3456789
            10111213141516
            17181920212223
            24252627282930
            31123456

            常用鏈接

            留言簿(1)

            隨筆檔案(15)

            搜索

            •  

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            久久青青草原综合伊人| 久久久久久久亚洲精品| 久久99热这里只有精品66| 午夜精品久久久久| 亚洲AV乱码久久精品蜜桃| 久久人妻少妇嫩草AV无码专区| 精品久久久久中文字幕日本| 一本一道久久精品综合| 久久经典免费视频| 久久综合久久综合久久| 中文字幕无码久久人妻| 精品久久久久久无码专区不卡| 99精品伊人久久久大香线蕉| 久久久久亚洲AV成人网人人网站 | 97久久精品午夜一区二区| 99久久国产热无码精品免费久久久久 | 天天久久狠狠色综合| 亚洲精品午夜国产va久久| 久久99热国产这有精品| 久久婷婷色香五月综合激情| 欧美伊香蕉久久综合类网站| 久久人与动人物a级毛片| 国产精品久久久天天影视香蕉 | 日韩久久久久久中文人妻| 久久亚洲中文字幕精品一区四| 久久精品国产亚洲AV电影| 久久99这里只有精品国产| 国内精品久久久久久久coent | 精品久久亚洲中文无码| 7国产欧美日韩综合天堂中文久久久久| 东方aⅴ免费观看久久av| 人人狠狠综合久久亚洲高清| 精品国产一区二区三区久久蜜臀| 久久精品国产亚洲精品2020| 久久久综合香蕉尹人综合网| 久久久久久久综合日本亚洲 | 国产精品久久久久久影院| 国产精品无码久久综合 | 伊色综合久久之综合久久| 无码精品久久久天天影视| 狠狠色丁香婷婷综合久久来来去|