• <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>
            隨筆 - 298  文章 - 377  trackbacks - 0
            <2025年5月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            常用鏈接

            留言簿(34)

            隨筆分類

            隨筆檔案

            文章檔案

            相冊

            收藏夾

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

             -0.271272   0.057637   -1.169941   -0.595656   -1.034938   -2.084308   -1.636656   -0.379287   0.004293   0.289059   0.519261   0.471439   0.571174   0.021626   0.110812   0.810365   0.856656   -0.149288   0.047479   0.215580   -0.163080   -0.832364   -1.017397   -0.769935   -0.434539   -0.555873   -0.462823   -0.662093   0.404535   0.167199   -0.156460   -0.272831   -0.438901   0.469157   0.484331   0.497634   0.012820   -0.359225   -1.001581   -0.702035   -1.427914   -1.775163   -1.531204   -1.519546   -1.839334   -1.734282   -0.865233   -1.666558   -2.260154   -1.655687   -1.768353   -2.065409   -1.846770   -1.859222   -1.720874


            #include   <fstream>  
              #include   <iostream>  
              #include   <stdio.h>  
              #include   <stdlib.h>  
              #include   <vector>  
              #include   <iterator>  
               
              using   namespace   std;  
              void   main()  
              {  
              vector<vector<double>   >   v;  
              ifstream   in("c:\\hh.dat");  
              double   tmp;  
               
              v.push_back(vector<double>());  
              vector<double>*   p   =   &v.back();  
              while(!in.eof()){  
              in   >>   tmp;  
              p->push_back(tmp);  
              if(in.peek()   ==   '\n'){  
              v.push_back(vector<double>());  
              p   =   &v.back();  
              }  
              }  
               
              for(int   i   =   0;   i   <   v.size();   ++i){  
              copy(v[i].begin(),   v[i].end(),   ostream_iterator<double>(cout,   "   "));  
              cout   <<   "#####****"   <<   endl;  
              }  




            #include   <iostream.h>  
              #include   <fstream.h>  
              #include   <afxtemplel.h>  
               
              main()  
              {  
                                FILE*     fp;  
              char       name[256];      
               
              strcpy(name,fileName.ConvertToChar());  
              cout   <<   name   <<   endl   <<   flush;  
              if((fp=fopen(name,"r"))==NULL)  
              {  
              cout<<"This     file     is     not     opened!"<<endl   <<   flush;  
              return;  
              }  
                                cout   <<   "open   file   success!"   <<   endl   <<   flush;  
              ifstream   in(name,ios::in);  
               
              int   i   =   0;  
              char   line[255];  
              char   *token   =   NULL;  
              char   seps[]       =   "   ,\t\n";//delimiters   in   the   asc   files“空格或逗號”分隔符  
              CArray<double,double&>   *line_mArr=new   CArray<double,double&>   [50];//最多50行  
              int   arr_counter=0;//行計數  
                                int   Point_Counter   =0;//點計數  
              while   (fgets(line,255,fp)!=NULL)  
              {  
              if(!strcmp(line,"\n"))   continue;//遇到空行,但是未到文件結尾。  
              token=strtok(line,seps);  
              while(token   =   strtok(NULL,seps)!=EOF)  
                                                                    {  
                                                                          line_mArr[arr_counter].Add(strtok(NULL,seps));//提取讀入的一個數的串到數組中  
                     
                                                  }  
                                                                    arr_counter++;  
              }  
                                for(int   j=0;j<arr_counter;j++)  
              {  
                    Point_Counter+=line_mArr[j].GetSize();  
              }  
              cout   <<   "Point   num   =   "<<   Point_Counter   <<endl;//輸出點的總數  
              fclose(fp);  
              return;  
              }  














            已修訂,enjoy   it!  
              #include   <fstream>  
              #include   <iostream>  
              #include   <stdio.h>  
              #include   <stdlib.h>  
              #include   <vector>  
              #include   <iterator>  
               
              using   namespace   std;  
              void   main()  
              {  
              vector<vector<double>   >   v;  
              ifstream   in("c:\\hh.dat");  
              double   tmp;  
              char   dummy;  
               
              v.push_back(vector<double>());  
              vector<double>*   p   =   &v.back();  
              while(!in.eof()){  
              while(in.peek()   ==   '   ')   in.read(&dummy,   1);   //eat   space  
              if(in.peek()   ==   '\n'){  
              v.push_back(vector<double>());  
              p   =   &v.back();  
              }  
              in   >>   tmp;  
              p->push_back(tmp);  
              }  
               
              cout   <<"\n==========   result   ============"   <<   endl;  
              cout.precision(10);  
              for(int   i   =   0;   i   <   v.size();   ++i){  
              copy(v[i].begin(),   v[i].end(),   ostream_iterator<double>(cout,   "   "));  
              cout   <<   "#####****"   <<   endl;  
              }  
               
              }
            posted on 2007-06-15 14:43 聶文龍 閱讀(4072) 評論(3)  編輯 收藏 引用 所屬分類: c++

            FeedBack:
            # re: 使用CFile或者ifstream讀入數據 2007-06-15 14:46 聶文龍
            char buf[255];
            ifstream ifs;
            ifs.open("dir");
            while(!ifs.eof())
            {
            ifs.getline(buf,254);
            //做響應處理,如
            }
            return 0;  回復  更多評論
              
            # re: 使用CFile或者ifstream讀入數據 2007-06-15 14:53 聶文龍
            #include <iostream>
            #include <string>
            #include <fstream>

            using namespace std;


            int main()
            {
            ifstream in("your_file");

            string line;

            while (getline(in,line))
            {
            cout << line << "\n";
            }

            return 0;
            }  回復  更多評論
              
            # re: 使用CFile或者ifstream讀入數據 2007-06-15 14:53 聶文龍
            讀取文字檔有很多方式,在此歸納出最精簡的程式寫法。

            若要一行一行的讀取文字檔,可使用以下寫法。




            #include
            #include
            #include

            using namespace std;

            int main() {
            ifstream inFile("books.txt");
            string line;

            while(getline(inFile,line)) {
            cout << line << endl;
            }

            inFile.close();

            return 0;
            }

            執行結果


            this is a book
            a book a book
            book
            請按任意鍵繼續 . . .

            若在一行一行讀取文字檔的同時,還想同時讀出每一個字串,可用以下寫法。




            #include
            #include
            #include
            #include

            using namespace std;

            int main() {
            ifstream inFile("books.txt");
            string line;

            while(getline(inFile,line)) {
            cout << line << endl;
            istringstream ss(line);
            string word;
            while(ss >> word) {
            cout << word << endl;
            }
            cout << endl;
            }

            inFile.close();

            return 0;
            }

            執行結果


            this is a book
            this
            is
            a
            book

            a book a book
            a
            book
            a
            book

            book
            book

            請按任意鍵繼續 . . .

            若只要讀取文字檔中的每個字,使用while()的方式,可直接處理字串。





            #include
            #include
            #include

            using namespace std;

            int main() {
            ifstream inFile("books.txt");
            string str;

            while(infile >> str)
            cout << str << endl;

            inFile.close();

            return 0;
            }

            另外一種方式,使用copy() algorithm將文字都讀到vector中,再做後續的加工處理,優點是程式超短,缺點是要多浪費一個vector。




            #include
            #include
            #include
            #include
            #include

            using namespace std;

            int main() {
            ifstream inFile("books.txt");
            vector svec;
            copy(istream_iterator(inFile), istream_iterator(), back_inserter(svec));
            copy(svec.begin(), svec.end(), ostream_iterator(cout,"\n"));

            inFile.close();

            return 0;
            }

            執行結果


            this
            is
            a
            book
            a
            book
            a
            book
            book
            請按任意鍵繼續 . . .
            (02/20/2007 更新) 有網友問我怎麼將文字檔讀到二維陣列處理,以下是處理的方式
            文字檔
            00001 Peter Hsiao 555.55
            00002 John Lin 222.12


            #include
            #include
            #include
            #include

            using namespace std;

            int main() {
            ifstream inFile("source.txt");
            const int xsize = 5;
            const int ysize = 2;
            string (*arr)[xsize] = new string[ysize][xsize];

            string line;
            int y = 0;
            while(getline(inFile,line)) {
            istringstream ss(line);
            string word;
            int x = 0;
            while(ss >> word) {
            arr[y][x] = word;
            ++x;
            }
            ++y;
            }

            inFile.close();

            for(int y = 0; y != ysize; ++y) {
            for(int x = 0; x != xsize; ++x) {
            cout << arr[y][x] << " ";
            }
            cout << endl;
            }

            delete []arr;

            return 0;
            }
            執行結果
            00001 Peter Hsiao 555.55
            00002 John Lin 222.12
              回復  更多評論
              
            久久精品国产亚洲AV蜜臀色欲| 国产成人久久精品麻豆一区| 久久久久久久久久久久久久| 久久久精品国产| 韩国免费A级毛片久久| 亚洲国产精品一区二区久久| 亚洲精品无码久久久| 久久99国产综合精品免费| 久久国产成人午夜aⅴ影院| 精品久久久一二三区| 91超碰碰碰碰久久久久久综合| 色综合合久久天天给综看| 新狼窝色AV性久久久久久| 久久久精品久久久久特色影视| 久久亚洲精品无码aⅴ大香| 久久精品嫩草影院| 性做久久久久久久| 久久久免费观成人影院| www久久久天天com| 国内精品综合久久久40p| 久久性精品| 国产精品久久久久久久午夜片| 久久亚洲精品国产精品| 亚洲Av无码国产情品久久| 精品熟女少妇aⅴ免费久久| 国产产无码乱码精品久久鸭| 一本色综合网久久| 99久久99久久精品国产片果冻 | 久久伊人五月丁香狠狠色| 久久久久久久尹人综合网亚洲| 久久人妻少妇嫩草AV无码专区| 日韩人妻无码一区二区三区久久99| 品成人欧美大片久久国产欧美...| 国产精品久久久久天天影视| 久久无码人妻一区二区三区午夜 | 国内精品久久久久久久久电影网| 色婷婷狠狠久久综合五月| 久久夜色精品国产| 久久人妻无码中文字幕| 无码日韩人妻精品久久蜜桃| 久久久久久九九99精品|