C读取文件

C读取文件

C读取文件,这种写法不会多一行。

 1 #include "stdafx.h"
 2 #include <vector>
 3 using namespace std;
 4 struct PointXYZ
 5 {
 6     double X;
 7     double Y;
 8     double Z;
 9 };
10 
11 int _tmain(int argc, _TCHAR* argv[])
12 {
13      FILE* in=fopen("D:\project60-cut-1000.txt","r");
14      if(in==NULL)
15      {
16          printf("missing file");
17          return 0;
18      }
19      double tmp=0;
20      //char buff[255] = {};
21      float x;
22      float y;
23      float z;
24      vector<PointXYZ> *points=new vector<PointXYZ>();
25      char* str;
26      int i=0;
27     // while(!feof(in))  
28      fscanf(in,"%f %f %f",&x,&y,&z); 
29      while(feof(in)==0) /*判断是否文件尾,不是则循环*/
30      {
31          i++;     
32          PointXYZ point;
33          point.X=x;
34          point.Y=y;
35          point.Z=z;
36          points->push_back(point);        
37          fscanf(in,"%f %f %f",&x,&y,&z); 
38      }
39      fclose(in);
40      
41      for (vector<PointXYZ>::iterator iter = points->begin(); iter != points->end(); ++iter)
42      {
43          PointXYZ tmp=(PointXYZ)*iter;
44          printf("%f %f %f
",tmp.X,tmp.Y,tmp.Z);
45      }
46      printf("Number:%d
",(int)points->size());
47       system("pause");//getchar();
48      return 0;
49 }

参考C++的读取

 1 void LoadImages(const string &strAssociationFilename, vector<string> &vstrImageFilenamesRGB,
 2                 vector<string> &vstrImageFilenamesD, vector<double> &vTimestamps)
 3 {
 4     ifstream fAssociation;
 5     fAssociation.open(strAssociationFilename.c_str());
 6     while(!fAssociation.eof())
 7     {
 8         string s;
 9         getline(fAssociation,s);
10         if(!s.empty())
11         {
12             stringstream ss;
13             ss << s;
14             double t;
15             string sRGB, sD;
16             ss >> t;
17             vTimestamps.push_back(t);
18             ss >> sRGB;
19             vstrImageFilenamesRGB.push_back(sRGB);
20             ss >> t;
21             ss >> sD;
22             vstrImageFilenamesD.push_back(sD);
23 
24         }
25     }
26 }