Advertisement
Guest User

other_functions_csv_loader

a guest
Aug 30th, 2013
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.18 KB | None | 0 0
  1. //get the count of the columns per line
  2. int *get_column_map(char *fn){
  3.     int l=lines(fn);
  4.     int *column_map=malloc(sizeof(int)*l);
  5.     FILE *file_c=fopen(fn, "r");
  6.     int character=0;
  7.     int cc=1, lc=0;
  8.     while(character!=EOF){
  9.         character=getc(file_c);
  10.         if(character==','){
  11.         cc++;
  12.         }else{
  13.             if(character=='\n'){
  14.             column_map[lc]=cc;
  15.             printf("%d\n", cc);
  16.             cc=1;
  17.             lc++;
  18.             }
  19.         }
  20.     }
  21.     fclose(file_c);
  22.     return column_map;
  23. }
  24.  
  25. //get the length of the first column of the csv file
  26. int getfirstcolumn(char *filename){
  27. FILE *firstcolumnopen=fopen(filename, "r");
  28. int x_=0;
  29. int pos=0;
  30.     while(x_!=EOF){
  31.     x_=getc(firstcolumnopen);
  32.         if(x_==','){
  33.         pos=ftell(firstcolumnopen);
  34.         fclose(firstcolumnopen);
  35.         return pos;
  36.         }else{
  37.         }
  38.     }
  39.     return 0;
  40. }
  41.  
  42. //counts the lines of the file
  43. int lines(char *file_){
  44.     FILE * file;
  45. file = fopen(file_, "r");
  46. int c=0;
  47. int counter=0;
  48.  
  49. int len=0;
  50. while(c!=EOF){
  51.     c=getc(file);
  52.     if(c=='\n'){
  53.         counter++;
  54.     }
  55.     len++;
  56. }
  57. fclose(file);
  58. return counter;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement