随笔 - 298  文章 - 377  trackbacks - 0
<2007年6月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
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 聂文龙 阅读(4031) 评论(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
  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理