Advertisement
d10070dd

csvread3

Sep 14th, 2013
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.77 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_1=0,newline_count_2=0,lines;
  10.     FILE *fpi1,*fpi2,*fpo;
  11.     float x,y,z;
  12.  
  13.  
  14.     if(argc!=4)
  15.     {
  16.         fprintf(stderr,"Usage: %s (1)CSV1File\n(2)CSV2File\n(3)VTKFile\n",argv[0]);
  17.  
  18.         return 0;
  19.     }
  20.  
  21.     printf("Open CSV1 File\n");
  22.     //Open CSV1 file
  23.     if((fpi1=fopen(argv[1],"r"))==NULL)
  24.     {
  25.         printf("The file can't be opened. The program is exit.\n");
  26.  
  27.         return 0;
  28.     }
  29.  
  30.     printf("Open CSV2 File\n");
  31.     //Open CSV2 file
  32.     if((fpi2=fopen(argv[2],"r"))==NULL)
  33.     {
  34.         printf("The file can't be opened. The program is exit.\n");
  35.  
  36.         return 0;
  37.     }
  38.  
  39.     printf("Open VTK File\n");
  40.     //Open VTK file for save
  41.     if((fpo=fopen(argv[3],"w"))==NULL)
  42.     {
  43.         printf("The file can't be opened. The program is exit.\n");
  44.  
  45.         return 0;
  46.     }
  47.  
  48.     //count how many lines
  49.     /*
  50.     while((c==fgetc(fpi))!=EOF
  51.     {
  52.         if(c=='\n')
  53.             newline_count++;
  54.     }
  55.     */
  56.  
  57.     printf("CSV1 File\n");
  58.     while (fscanf(fpi1,"%f,%f,%f,%d,%d,%d", &x, &y, &z, &r, &g, &b)!= EOF)
  59.     {
  60.  
  61.         newline_count_1=newline_count_1+1;
  62.     }
  63.  
  64.     printf("CSV1 total=%ld\n",newline_count_1);
  65.  
  66.     printf("CSV2 File\n");
  67.     while (fscanf(fpi2,"%f,%f,%f,%d,%d,%d", &x, &y, &z, &r, &g, &b)!= EOF)
  68.     {
  69.  
  70.         newline_count_2=newline_count_2+1;
  71.     }
  72.  
  73.     printf("CSV2 total=%ld\n",newline_count_2);
  74.  
  75.     lines=newline_count_1+newline_count_2;
  76.  
  77.     printf("Total lines=%ld\n",lines);
  78.  
  79.     //writing VTK file
  80.     fprintf(fpo,"# vtk DataFile Version 3.0\n");
  81.     fprintf(fpo,"vtk output\n");
  82.     fprintf(fpo,"ASCII\n");
  83.     fprintf(fpo,"DATASET UNSTRUCTURED_GRID\n");
  84.     fprintf(fpo,"POINTS %ld float\n",lines);
  85.  
  86.     fseek(fpi1,0,SEEK_SET);
  87.     fseek(fpi2,0,SEEK_SET);
  88.  
  89.     printf("writing CSV1 x y z\n");
  90.     while (fscanf(fpi1,"%f,%f,%f,%d,%d,%d", &x, &y, &z, &r, &g, &b)!= EOF)
  91.     {
  92.         printf("%f,%f,%f,%d,%d,%d\n", x, y, z, r, g, b);
  93.         fprintf(fpo,"%f %f %f\n",x,y,z);
  94.     }
  95.  
  96.     printf("writing CSV2 x y z\n");
  97.     while (fscanf(fpi2,"%f,%f,%f,%d,%d,%d", &x, &y, &z, &r, &g, &b)!= EOF)
  98.     {
  99.         printf("%f,%f,%f,%d,%d,%d\n", x, y, z, r, g, b);
  100.         fprintf(fpo,"%f %f %f\n",x,y,z);
  101.     }
  102.  
  103.     fprintf(fpo,"CELLS %ld %ld\n",lines,lines*2);
  104.  
  105.     for(i=0;i<lines;i++)
  106.     {
  107.         fprintf(fpo,"1 %d\n",i);
  108.     }
  109.  
  110.     fprintf(fpo,"CELL_TYPES %ld\n",lines);
  111.  
  112.     for(i=0;i<lines;i++)
  113.     {
  114.         fprintf(fpo,"1\n");
  115.     }
  116.  
  117.     fprintf(fpo,"POINT_DATA %ld\n",lines);
  118.  
  119.     fprintf(fpo,"COLOR_SCALARS RGB 3\n");
  120.  
  121.     fseek(fpi1,0,SEEK_SET);
  122.     fseek(fpi2,0,SEEK_SET);
  123.  
  124.     while (fscanf(fpi1,"%f,%f,%f,%d,%d,%d", &x, &y, &z, &r, &g, &b)!= EOF)
  125.     {
  126.  
  127.         fprintf(fpo,"%f %f %f\n",(float)r/255,(float)g/255,(float)b/255);
  128.     }
  129.  
  130.     while (fscanf(fpi2,"%f,%f,%f,%d,%d,%d", &x, &y, &z, &r, &g, &b)!= EOF)
  131.     {
  132.  
  133.         fprintf(fpo,"%f %f %f\n",(float)r/255,(float)g/255,(float)b/255);
  134.     }
  135.  
  136.     fclose(fpi1);
  137.     fclose(fpi2);
  138.     fclose(fpo);
  139.  
  140.  
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement