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>
- #include <string.h>
- char dirname[256],fname[256],path[512];
- char* selectFile() {
- DIR *dir = NULL;
- FILE *f;
- struct dirent *pent = NULL;
- while(dirname != 0){
- char *dirname = "G:/cfile";
- dir = opendir(dirname);
- printf("\nChoose file:\n");
- while((pent= readdir(dir))!=NULL){
- printf("%s\n",pent->d_name);
- }
- scanf("%s",fname);
- char *extn = strrchr(fname, '.');
- while(!strcmp(extn, ".c") == 0) {
- printf("\nFile must be with extension .c, please choose file with extension .c:\n");
- dir = opendir(dirname);
- 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 longestShortestLine()
- {
- int charsInLIne = 0,lineMin = 0,lineMax=0,line=0,c;
- FILE *fp;
- fp = fopen(path,"r");
- c = getc(fp);
- printf("\n");
- int max = 0;
- int min = 2000000;
- while(c != EOF){
- c = getc(fp);
- if(c == '\n' && charsInLIne > 0){
- line++;
- if(charsInLIne > max){
- max = charsInLIne;
- lineMax = line;
- }
- if(charsInLIne < min){
- min = charsInLIne;
- lineMin = line;
- }
- charsInLIne = 0;
- }
- if(c != '\n'){
- charsInLIne++;
- }
- }
- printf("Longest line chars: %d at line: %d\n",min,lineMin);
- printf("Shortest line chars: %d at line: %d\n",max,lineMax);
- fclose(fp);
- }
- 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;
- while(c != EOF){
- c = fgetc(selectedFile);
- if(c == '\n'){
- newLine = 1;
- }
- if(c == '/' && newLine == 1){
- c = fgetc(selectedFile);
- if(c == '/'){
- comments++;
- newLine = 0;
- }
- }
- }
- printf("\n");
- printf("\nAll comment in %s are:%d\n",fname, comments);
- }
- int main ()
- {
- char menu;
- do{
- printf("Selected file: %s",fname);
- printf("\n1. Select a file: \n");
- printf("2. Longest and Shortest Line\n");
- printf("3. Average symbols in rows : all symbols \n");
- printf("4. Shows the number of inline comments //.\n");
- printf("5. Quit\n");
- menu = getche();
- switch(menu){
- case '1': selectFile();break;
- case '2': longestShortestLine();break;
- case '3': avgChRowToAllCh();break;
- case '4': countComments( );break;
- case '5': printf("\n\Quit");break;
- default: printf("\n\nPlease enter number between 1-5");
- }
- }while(menu!= '5');
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment