veselinsavov

Untitled

Jan 21st, 2015
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.25 KB | None | 0 0
  1. #include <dirent.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #include <string.h>            
  6.  
  7. char dirname[256],fname[256],path[512];
  8. char* selectFile() {
  9.         DIR *dir = NULL;
  10.         FILE *f;
  11.     struct dirent *pent = NULL;
  12.         while(dirname != 0){
  13.                 char *dirname = "G:/cfile";
  14.                 dir = opendir(dirname);
  15.                 printf("\nChoose file:\n");
  16.             while((pent= readdir(dir))!=NULL){
  17.                                 printf("%s\n",pent->d_name);
  18.                         }
  19.                 scanf("%s",fname);
  20.                 char *extn = strrchr(fname, '.');
  21.                 while(!strcmp(extn, ".c") == 0) {                      
  22.                         printf("\nFile must be with extension .c, please choose file with extension .c:\n");
  23.                         dir = opendir(dirname);
  24.                         while((pent= readdir(dir))!=NULL){
  25.                                 printf("%s\n",pent->d_name);
  26.                         }
  27.                         scanf("%s",fname);
  28.                         }
  29.                
  30.                 char wholeName[512];
  31.                 strcpy(wholeName,dirname);
  32.                 strcat(wholeName,"/");
  33.                 strcat(wholeName,fname);
  34.                 strcat(path,wholeName);
  35.            
  36.                 while((f=fopen(wholeName,"r"))==NULL){
  37.                                 printf("The file does not exist!\n please choose file: ");
  38.                                 scanf("%s",fname);
  39.                                 strcpy(wholeName,dirname);
  40.                                 strcat(wholeName,"/");
  41.                                 strcat(wholeName,fname);
  42.                                 strcat(path,wholeName);
  43.                 }
  44.                 closedir (dir);
  45.                 return dir;
  46.         }
  47. }
  48. void longestShortestLine()
  49. {
  50.                 int charsInLIne = 0,lineMin = 0,lineMax=0,line=0,c;
  51.         FILE *fp;
  52.         fp = fopen(path,"r");
  53.         c = getc(fp);
  54.         printf("\n");
  55.                 int max = 0;
  56.                 int min = 2000000;
  57.         while(c != EOF){
  58.                 c = getc(fp);
  59.                 if(c == '\n' && charsInLIne > 0){
  60.                                                 line++;
  61.                                                 if(charsInLIne > max){
  62.                                                         max = charsInLIne;
  63.                                                         lineMax = line;
  64.                                                         }
  65.                                                 if(charsInLIne < min){
  66.                                                         min = charsInLIne;
  67.                                                         lineMin = line;
  68.                                                         }
  69.                                         charsInLIne = 0;
  70.                 }
  71.                                 if(c != '\n'){
  72.                                         charsInLIne++;
  73.                                         }
  74.                
  75.         }
  76.                 printf("Longest line chars: %d at line: %d\n",min,lineMin);
  77.                 printf("Shortest line chars: %d at line: %d\n",max,lineMax);
  78.         fclose(fp);
  79. }
  80.  
  81. void avgChRowToAllCh()
  82. {
  83.         int charsInLIne = 0,chars = 0,c;
  84.         FILE *selectedFile;
  85.         selectedFile = fopen(path,"r");
  86.         c = getc(selectedFile);        
  87.        
  88.         while (c != EOF)
  89.         {
  90.                 chars++;      
  91.                 c = getc(selectedFile);
  92.         }
  93.         fclose(selectedFile);
  94.         FILE *fp;
  95.         fp = fopen(path,"r");
  96.         c = getc(fp);
  97.         int line = 1;
  98.         printf("\n");
  99.         printf("-------------------------------------------------\n");
  100.         printf("Line       *   Chars in line       All chars\n");
  101.         printf("-------------------------------------------------\n");
  102.        
  103.         while(c != EOF){
  104.                 c = getc(fp);
  105.                 if(c == '\n'){
  106.                         printf("Line:%-5d *   Chars in line:%-5d All chars:%d  \n",line,charsInLIne,chars);
  107.                         printf("-------------------------------------------------\n");
  108.                         charsInLIne = 0;
  109.                         line ++;
  110.                 }
  111.                 charsInLIne++;
  112.                
  113.         }
  114.         fclose(fp);
  115. }
  116.  
  117. void countComments()
  118. {
  119.         int comments = 0, c;
  120.         FILE *selectedFile;
  121.         selectedFile = fopen(path,"r");
  122.         c = getc(selectedFile);
  123.         int newLine = 1;
  124.         while(c != EOF){
  125.                         c = fgetc(selectedFile);
  126.                         if(c == '\n'){
  127.                                         newLine = 1;
  128.                         }
  129.                         if(c == '/' && newLine == 1){
  130.                                         c = fgetc(selectedFile);
  131.                                         if(c == '/'){
  132.                                                         comments++;
  133.                                                         newLine = 0;
  134.                                                         }
  135.                                         }
  136.         }
  137.         printf("\n");
  138.         printf("\nAll comment in %s are:%d\n",fname, comments);
  139. }
  140. int main ()
  141. {
  142.         char menu;
  143.         do{
  144.                         printf("Selected file: %s",fname);
  145.                         printf("\n1. Select a file: \n");
  146.                         printf("2. Longest  and Shortest Line\n");
  147.                         printf("3. Average symbols in rows : all symbols \n");
  148.                         printf("4. Shows the number of inline comments //.\n");
  149.                         printf("5. Quit\n");
  150.                         menu = getche();
  151.                         switch(menu){
  152.                                         case '1': selectFile();break;
  153.                                         case '2': longestShortestLine();break;
  154.                                         case '3': avgChRowToAllCh();break;
  155.                                         case '4': countComments( );break;
  156.                                         case '5': printf("\n\Quit");break;
  157.                                         default: printf("\n\nPlease enter number between 1-5");
  158.                         }
  159.         }while(menu!= '5');
  160.         return 0;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment