c-programming

Simple C programs
git clone git://git.laack.co/c-programming.git
Log | Files | Refs

count-columns.c (829B)


      1 #include <stdio.h>
      2 
      3 int countColumns(char* filename){
      4     FILE* fp = fopen(filename, "r");
      5     char buffer[1024];
      6     int columns = 0;
      7     int done = -1;
      8     int currentChars = 0;
      9     while(fgets(buffer, 1024, fp) && done == -1){
     10         for(int i = 0; i < 1024 ; ++i){
     11             if(buffer[i] == ','){
     12                 columns += 1;
     13                 currentChars = 0;
     14             }
     15             else if(buffer[i] == '\n' || buffer[i] == 0 || buffer[i] == EOF){
     16                 done = 0;
     17                 break;
     18             }
     19             else{
     20                 currentChars += 1;
     21             }
     22         }
     23     }
     24     if (currentChars > 0){
     25         columns += 1;
     26     }
     27     return columns;
     28 }
     29 
     30 //int main(int argc, char** argv){
     31 //
     32 //    int columnCount = countColumns(argv[1]);
     33 //    printf("%d\n\n", columnCount);
     34 //
     35 //    return 0;
     36 //}