Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #define VALID_ARGS 2
- #define MAX_WORD 45
- #define ARGS "FILENAME"
- int main(int argc, char *argv[]){
- int count = 0;
- int wcount = 0;
- char *line;
- FILE *fp;
- if(argc != VALID_ARGS){
- printf("%s %s\n",argv[0], ARGS );
- exit(EXIT_FAILURE);
- }
- if((fp = fopen(argv[1], "r")) == NULL) {
- perror("fopen");
- exit(EXIT_FAILURE);
- }
- if((line = (char *)malloc (sizeof(char) * MAX_WORD)) == NULL){
- perror("malloc line");
- exit(EXIT_FAILURE);
- }
- while((fgets(line, MAX_WORD, fp)) != NULL){
- line[strlen(line)-1] = '\0';
- printf("%s:%d\n",line, strlen(line));
- count += strlen(line);
- wcount++;
- }
- printf("total wordcount: %d\n", wcount);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement