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

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

                下面演示了如何將這種對象與特定的文件關(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()接受一個C-風(fēng)格字符串作為參數(shù),這可以是一個字面字符串,也可以是存儲在數(shù)組中的字符串。
                下面演示了如何使用這種對象:

                    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

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

                總之,使用文件輸出的主要步驟如下:
                1. 包含頭文件fstream。
                2. 創(chuàng)建一個ofstream對象。
                3. 將該ofstream對象同一個文件關(guān)聯(lián)起來。
                4. 就像使用count那樣使用該ofstream對象。
                程序清單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ù)是浮點數(shù).則按定點表示法輸出
                    outFile.precision(2);//按照指定的精度控制浮點數(shù)輸出
                    outFile.setf(ios_base::showpoint);//為輸出的數(shù)據(jù)強(qiáng)制加小數(shù)點以及小數(shù)點后
                    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而已。下面是該程序的運行情況:
                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í)行文件所在的目錄,將看到一個名為carinfo.txt的新文件,其中包含使用outFile生成的輸出。
               
                程序說明
                在程序清單6.15的程序中,聲明一個ofstream對象后,便可以使用方法open()來將該對象同一個特定的文件關(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ù),這是因為outFile已經(jīng)同特定的文件關(guān)聯(lián)起來了。如果您忘記關(guān)閉文件,程序正常終止時將自動關(guān)閉它。
                outFile可使用cout可使用的任何方法。它不但能夠使用操作符<<,還可以使用各種格式化方法,如setf()和precision()。這些方法只影響調(diào)用它們的對象。例如,對于不同的對象,可以提供不同的值:

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

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

                    outFile.open("carinfo.txt");

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

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

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

            posted on 2010-10-07 19:10 王秋林 閱讀(1397) 評論(0)  編輯 收藏 引用
            <2025年5月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            常用鏈接

            留言簿(1)

            隨筆檔案(15)

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            av色综合久久天堂av色综合在| 久久人搡人人玩人妻精品首页| 久久99精品国产麻豆婷婷| 天天爽天天狠久久久综合麻豆| 欧美精品福利视频一区二区三区久久久精品 | 曰曰摸天天摸人人看久久久| 国产aⅴ激情无码久久| 国内精品综合久久久40p| 漂亮人妻被中出中文字幕久久 | 久久久久亚洲av成人无码电影| 久久97精品久久久久久久不卡| 久久精品夜夜夜夜夜久久| 久久99精品国产麻豆| 99久久精品费精品国产一区二区| 丰满少妇高潮惨叫久久久| 996久久国产精品线观看| 久久亚洲综合色一区二区三区| 久久成人精品视频| 爱做久久久久久| 一级做a爰片久久毛片免费陪| 日本精品久久久久影院日本 | 国产精品久久久久9999| 青青青青久久精品国产 | www性久久久com| 久久精品?ⅴ无码中文字幕| 香蕉久久影院| av午夜福利一片免费看久久| 99久久人人爽亚洲精品美女| 久久夜色撩人精品国产| 2021久久精品免费观看| 国产精品久久久久影院嫩草 | 久久久无码精品午夜| 99久久国产精品免费一区二区| 91精品国产高清91久久久久久| 久久人妻少妇嫩草AV蜜桃| 亚洲午夜无码久久久久| 一本一道久久精品综合| 久久狠狠爱亚洲综合影院| 亚洲综合婷婷久久| 无码人妻久久一区二区三区免费丨| 91精品国产综合久久四虎久久无码一级 |