Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <dirent.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- char dirname[256],fname[256],path[512];
- char* chooseDir() {
- DIR *dir = NULL;
- FILE *f;
- struct dirent *pent = NULL;
- while(dirname != 0){
- printf(".Choose directory and file:");
- scanf("%s",dirname);
- dir = opendir(dirname);
- printf("%s",dirname);
- printf("Choose file:\n");
- while((pent= readdir(dir))!=NULL){
- printf("%s\n",pent->d_name);
- }
- scanf("%s",fname);
- char wholeName[512];
- strcpy(wholeName,dirname);
- strcat(wholeName,"/");
- strcat(wholeName,fname);
- strcat(path,wholeName);
- while((f=fopen(wholeName,"r"))==NULL){
- printf("The file does not exist!\n please choose file: ");
- scanf("%s",fname);
- strcpy(wholeName,dirname);
- strcat(wholeName,"/");
- strcat(wholeName,fname);
- strcat(path,wholeName);
- }
- closedir (dir);
- return dir;
- }
- }
- void countLines()
- {
- int c;
- int lines = 0;
- FILE *selectedFile;
- selectedFile = fopen(path,"r");
- c = getc(selectedFile);
- while (c != EOF)
- {
- if(c == '\n') lines++;
- c = getc(selectedFile);
- }
- fclose(selectedFile);
- printf("\n\nRows in %s are : %d\n",fname, lines);
- }
- void avgChRowToAllCh()
- {
- int charsInLIne = 0,chars = 0,c;
- FILE *selectedFile;
- selectedFile = fopen(path,"r");
- c = getc(selectedFile);
- while (c != EOF)
- {
- chars++;
- c = getc(selectedFile);
- }
- fclose(selectedFile);
- FILE *fp;
- fp = fopen(path,"r");
- c = getc(fp);
- int line = 1;
- printf("\n");
- printf("-------------------------------------------------\n");
- printf("Line * Chars in line All chars\n");
- printf("-------------------------------------------------\n");
- while(c != EOF){
- c = getc(fp);
- if(c == '\n'){
- printf("Line:%-5d * Chars in line:%-5d All chars:%d \n",line,charsInLIne,chars);
- printf("-------------------------------------------------\n");
- charsInLIne = 0;
- line ++;
- }
- charsInLIne++;
- }
- fclose(fp);
- }
- void countComments()
- {
- int comments = 0, c;
- FILE *selectedFile;
- selectedFile = fopen(path,"r");
- c = getc(selectedFile);
- int newLine = 1;
- int left = 0;
- int right = 0;
- while(c != EOF){
- c = fgetc(selectedFile);
- if(c == '\n'){
- newLine = 1;
- }
- if(c == '/' && newLine == 1){
- c = fgetc(selectedFile);
- if(c == '/'){
- comments++;
- newLine = 0;
- }
- }
- }
- //
- FILE *fp;
- fp = fopen(path,"r");
- c = getc(fp);
- while(c != EOF){
- c = fgetc(fp);
- if(c == '/'){
- c = fgetc(fp);
- if(c == '*'){
- left++;
- }
- }
- if(c == '*'){
- c = fgetc(fp);
- if(c == '/'){
- right++;
- }
- }
- }
- int commentsToAdd,diff = 0;
- if(left > right){
- diff = abs(left - right);
- commentsToAdd = left - diff;
- } else if(right > left){
- diff = abs(left - right);
- commentsToAdd = (right - diff);
- } else {
- commentsToAdd = left;
- }
- comments += commentsToAdd;
- printf("\n");
- printf("\nAll comment in %s are:%d\n",fname, comments);
- }
- int main ()
- {
- char menu;
- do{
- printf("---Directory: %s selected file: %s",dirname,fname);
- printf("\n1. Select a directory: \n");
- printf("2. Number of rows in program\n");
- printf("3. Average symbols in rows : all symbols \n");
- printf("4. Shows the number of all comments.\n");
- printf("5. Quit\n");
- menu = getche();
- switch(menu){
- case '1': chooseDir();break;
- case '2': countLines();break;
- case '3': avgChRowToAllCh();break;
- case '4': countComments( );break;
- case '5': printf("\n\Bye Bye !!!");break;
- default: printf("\n\nPlease enter number between 1-5");
- }
- }while(menu!= '5');
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment