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\n",tmp.X,tmp.Y,tmp.Z);
45      }
46      printf("Number:%d\n",(int)points->size());
47       system("pause");//getchar(); 48      return 0;
49 }

 

你可能感兴趣的:(C读取文件)