• <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>

              C++博客 :: 首頁 :: 聯系 :: 聚合  :: 管理
              117 Posts :: 2 Stories :: 61 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(8)

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜


            第一個程序:將內容寫入txt文本

            #include "stdafx.h"
            #include 
            <iostream>
            #include 
            <fstream>  //for file I/O
            using namespace std;
            int main(int argc, char* argv[])
            {
                
            char automobile[50];
                
            int year;
                
            double a_price;
                
            double d_price;

                ofstream outFile; 
            //creat object for output

                
            /* associate with a file在這里,程序運行之前,文件carinfo.txt并不存在。
                 * 在這種情況下,方法open()將新建一個名為carinfo.txt的文件。運行該程序
                 * 之后,文件carinfo.txt將存在。默認情況下,open()將首先截短該文件,即
                 * 將其長度截短到零——丟棄原有內容,然后將新的輸出加入到該文件中。
                 *
                 * 打開已有的文件,以接受輸出時,默認將它其長度截短為零,因此原來的內容將丟失。
                 
            */

                outFile.open(
            "carinfo.txt"); 
                

                
                cout
            <<"Enter the make and model of auto mobile: ";
                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 cout

                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;
                cout.precision(
            2);
                cout.setf(ios_base::showpoint);
                outFile
            <<"Make and mode: "<<automobile<<endl;
                outFile
            <<"Year: "<<year<<endl;
                outFile
            <<"Was asking $"<<a_price<<endl;
                outFile
            <<"Now asing $"<<d_price<<endl;

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

            第二個程序:讀取txt文本的內容 
                  下面程序打開用戶指定的文件,讀取其中的數字,然后指出文件中包含多少個值以及它們的和與平均值。正確地設計輸入循環至關重要。
                   這個程序我運行的時候出了一個問題,就是未能找到最后結果計算,如下圖所示,
                  正確的運行結果應該是:
                   很有意思,可以研究哈,我在csdn發帖詢問了,可參考其帖子:txt文件讀取問題
                   程序代碼如下:
            #include "stdafx.h"
            #include 
            <iostream>
            #include 
            <fstream>    //file I/O support
            #include <cstdlib>  //support for exit()
            using namespace std;
            const int SIZE =60;
            int main(int argc, char* argv[])
            {
                
            char filename[SIZE];
                ifstream inFile;  
            //object for handling file input

                cout
            <<"Enter name of data file: ";
                cin.getline(filename, SIZE);
                inFile.open(filename); 
            //associate inFile with a file
                if(!inFile.is_open())  //failed to open file
                {
                    cout
            <<"Could not open the file "<<filename<<endl;
                    cout
            <<"Program terminating.\n";
                    exit(EXIT_FAILURE);
                }

                
            double value;
                
            double sum=0.0;
                
            int count=0;  //number of items read

                inFile
            >>value;  //get first value
                while(inFile.good()) //while input good and not at EOF
                {
                    
            ++count;            //one more item read
                    sum+=value;            //calculate runing total
                    
            //cout<<"第"<<count<<"個數="<<value<<endl;
                    inFile>>value;        //get next value
                }

                
            //cout<<value<<endl;
                if(inFile.eof())
                    cout
            <<"End of file reached.\n";
                
            else if(inFile.fail())
                    cout
            <<"Input terminated by data mismatch.\n";
                
            else
                    cout
            <<"Input terminated for unknown reason.\n";
                
            if(count==0)
                    cout
            <<"No data processed.\n";
                
            else
                
            {
                    cout
            <<"Items read: "<<count<<endl;
                    cout
            <<"Sum: "<<sum<<endl;
                    cout
            <<"Average: "<<sum/count<<endl;
                }

                inFile.close();        
            //finished with the file
                return 0;
            }
            posted on 2010-02-11 17:10 煙皚 閱讀(410) 評論(0)  編輯 收藏 引用 所屬分類: C++ primer plus學習筆記
            无码专区久久综合久中文字幕| 国内精品久久久久影院亚洲| 久久亚洲熟女cc98cm| 久久精品一区二区国产| 日韩精品久久久肉伦网站| 久久久久人妻一区精品色 | 久久丫忘忧草产品| 久久精品国产精品亚洲人人| 亚洲欧美成人久久综合中文网| 中文字幕人妻色偷偷久久| 国产亚洲综合久久系列| 久久国产免费| 久久99精品免费一区二区| 午夜人妻久久久久久久久| 亚洲国产高清精品线久久 | 国产精品久久久久一区二区三区| 无码8090精品久久一区| 精品久久久久久亚洲精品 | 久久99精品久久久久久不卡| 国产精品久久久久一区二区三区| 国产精品无码久久久久| 中文精品久久久久人妻| 人妻少妇久久中文字幕一区二区 | 人妻无码αv中文字幕久久琪琪布 人妻无码久久一区二区三区免费 人妻无码中文久久久久专区 | jizzjizz国产精品久久| 久久激情五月丁香伊人| 久久人人爽人人人人片av| 久久久久亚洲AV综合波多野结衣| 国内精品久久久久影院优| 国产精品久久久久久福利漫画| 国产精品久久久久久久久软件| 亚洲人成网亚洲欧洲无码久久| 久久精品国产69国产精品亚洲| 免费一级欧美大片久久网| 亚洲成色www久久网站夜月| 亚洲国产精品婷婷久久| www.久久热.com| 蜜桃麻豆www久久国产精品| 精品国产一区二区三区久久久狼| 久久99热这里只有精品国产| 亚洲精品无码久久久久|