d10070dd

csvread

Sep 14th, 2013
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.81 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int main(int argc, char *argv[])
  4. {
  5.  
  6.     //Decralation of variables
  7.     int i;
  8.     int r,g,b;
  9.     unsigned long newline_count=0;
  10.     FILE *fpi,*fpo;
  11.     float x,y,z;
  12.  
  13.  
  14.     if(argc!=3)
  15.     {
  16.         fprintf(stderr,"Usage: %s (1)CSVFile\n(2)VTKFile\n",argv[0]);
  17.  
  18.         return 0;
  19.     }
  20.  
  21.     //Open CSV file
  22.     if((fpi=fopen(argv[1],"r"))==NULL)
  23.     {
  24.         printf("The file can't be opened. The program is exit.\n");
  25.  
  26.         return 0;
  27.     }
  28.  
  29.     //Open VTK file for save
  30.     if((fpo=fopen(argv[2],"w"))==NULL)
  31.     {
  32.         printf("The file can't be opened. The program is exit.\n");
  33.  
  34.         return 0;
  35.     }
  36.  
  37.     //count how many lines
  38.     /*
  39.     while((c==fgetc(fpi))!=EOF
  40.     {
  41.         if(c=='\n')
  42.         newline_count++;
  43.     }
  44.     */
  45.  
  46.     while (fscanf(fpi,"%f,%f,%f,%d,%d,%d", &x, &y, &z, &r, &g, &b)!= EOF)
  47.     {
  48.  
  49.         newline_count=newline_count+1;
  50.     }
  51.  
  52.  
  53.     printf("total=%ld\n",newline_count);
  54.  
  55.     //writing VTK file
  56.     fprintf(fpo,"# vtk DataFile Version 3.0\n");
  57.     fprintf(fpo,"vtk output\n");
  58.     fprintf(fpo,"ASCII\n");
  59.     fprintf(fpo,"DATASET UNSTRUCTURED_GRID\n");
  60.     fprintf(fpo,"POINTS %ld float\n",newline_count);
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.     fseek(fpi,0,SEEK_SET);
  72.  
  73.     while (fscanf(fpi,"%f,%f,%f,%d,%d,%d", &x, &y, &z, &r, &g, &b)!= EOF)
  74.     {
  75.         printf("%f,%f,%f,%d,%d,%d\n", x, y, z, r, g, b);
  76.         fprintf(fpo,"%f %f %f\n",x,y,z);
  77.     }
  78.  
  79.     fprintf(fpo,"CELLS %ld %ld\n",newline_count,newline_count*2);
  80.  
  81.     for(i=0;i<newline_count;i++)
  82.     {
  83.         fprintf(fpo,"1 %d\n",i);
  84.     }
  85.  
  86.     fprintf(fpo,"CELL_TYPES %ld\n",newline_count);
  87.  
  88.     for(i=0;i<newline_count;i++)
  89.     {
  90.         fprintf(fpo,"1\n");
  91.     }
  92.  
  93.     fprintf(fpo,"POINT_DATA %ld\n",newline_count);
  94.  
  95.     fprintf(fpo,"COLOR_SCALARS RGB 3\n");
  96.  
  97.     fseek(fpi,0,SEEK_SET);
  98.  
  99.     while (fscanf(fpi,"%f,%f,%f,%d,%d,%d", &x, &y, &z, &r, &g, &b)!= EOF)
  100.     {
  101.  
  102.         fprintf(fpo,"%f %f %f\n",(float)r/255,(float)g/255,(float)b/255);
  103.     }
  104.  
  105.  
  106.  
  107.     fclose(fpi);
  108.     fclose(fpo);
  109.  
  110.  
  111. }
Advertisement
Add Comment
Please, Sign In to add comment