Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- int main(int argc, char *argv[])
- {
- //Decralation of variables
- int i;
- int r,g,b;
- unsigned long newline_count=0;
- FILE *fpi,*fpo;
- float x,y,z;
- if(argc!=3)
- {
- fprintf(stderr,"Usage: %s (1)CSVFile\n(2)VTKFile\n",argv[0]);
- return 0;
- }
- //Open CSV file
- if((fpi=fopen(argv[1],"r"))==NULL)
- {
- printf ("The file can't be opened. The program is exit.\n");
- return 0;
- }
- //Open VTK file for save
- if((fpo=fopen(argv[2],"w"))==NULL)
- {
- printf("The file can't be opened. The program is exit.\n");
- return 0;
- }
- //count how many lines
- /*
- while((c=fgetc(fpi))!=EOF)
- {
- if(c=='\n')
- newline_count++;
- }
- */
- while(fscanf(fpi,"%f,%f,%f,%d,%d,%d",&x,&y,&z,&r,&g,&b)!=EOF)
- {
- newline_count=newline_count+1;
- }
- printf("total=%ld\n",newline_count);
- //writing VTK file
- fprintf(fpo,"# vtk DataFile Version 3.0\n");
- fprintf(fpo,"vtk output\n");
- fprintf(fpo,"ASCII\n");
- fprintf(fpo,"DATASET UNSTRUCTURED_GRID\n");
- fprintf(fpo,"POINTS %ld float\n",newline_count);
- fseek(fpi,0,SEEK_SET);
- while (fscanf(fpi,"%f,%f,%f,%d,%d,%d",&x,&y,&z,&r,&g,&b)!=EOF)
- {
- printf("%f,%f,%f,%d,%d,%d\n",x,y,z,r,g,b);
- fprintf(fpo,"%f%f%f\n",x,y,z);
- }
- fprintf(fpo,"CELLS %ld %ld\n",newline_count,newline_count*2);
- for(i=0;i<newline_count;i++)
- {
- fprintf(fpo,"1 %d/n",i);
- }
- fprintf(fpo,"CELL_TYPES %ld\n",newline_count);
- for(i=0;i<newline_count;i++)
- {
- fprintf(fpo,"1\n");
- }
- fprintf(fpo,"POINT_DATA %ld\n",newline_count);
- fprintf(fpo,"COLOR_SCALARS RGB 3\n");
- fseek(fpi,0,SEEK_SET);
- while(fscanf(fpi,"%f,%f,%f,%d,%d,%d",&x,&y,&z,&r,&g,&b)!=EOF)
- {
- fprintf(fpo,"%f %f %f\n",(float)r/255,(float)g/255,(float)b/255);
- }
- fclose(fpi);
- fclose(fpo);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement