Help with program to list all the chars/letters/word in "/usr/share/dict/words"

This is a discussion on Help with program to list all the chars/letters/word in "/usr/share/dict/words" within the Linux Programming forums, part of the Platform Specific Boards category; Hello, We've been assigned a task where we have to create a piece of code to find all the characters/letters/words ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    2

    Help with program to list all the chars/letters/word in "/usr/share/dict/words"

    Hello,

    We've been assigned a task where we have to create a piece of code to find all the characters/letters/words in "/usr/share/dict/words" in Linux, using the Terminal and a Makefile.

    Here is what I have so far, and I can't figure out why it isn't compiling correctly or finding what I want it too:

    Code:
     
    
    #include <getopt.h>
    #include <stdlib.h>
    #include <stdio.h> 
    #include <string.h>
    #include <ctype.h>
    #include <unistd.h>
    
    
    #define BUFFSIZE 1000000
    
    
    long read_file(char *filename, char *text ) {
        FILE *fp   = fopen(filename, "r");
        long  size = 0; 
        int   len  = 0;
    
    
        if (fp == NULL) {
            fprintf(stderr,"Error could not open file: %s\n", strerror);
            return -1;
        }
       
        if (fseek(fp, 0L, SEEK_END) == 0) {
        
            size = ftell(fp);
            if (size == -1) {
                fprintf(stderr,"Error could not open file: %s\n",strerror);
                return -1;
            }
         
            if (fseek(fp, 0L, SEEK_SET) != 0) {
                fprintf(stderr, "Error rewinding to start of file: %\n",strerror);
                return -1;
            }
              
            len = fread(text, sizeof(char), (size_t)size, fp);
            if (len == 0) {
                fprintf(stderr, "Error reading file into memory: %s\n", strerror);
                return -1;
            } else {
               text[++len] = '\0';
            }
        }
        (void)fclose(fp);
        return size;
    }
    
    
    int chars_chars(char* buffer, int size) {
        
        return size / sizeof(char);
    
    
    }
    
    
    int words_words(char* buffer, int size) {
    
    
        int word_count = 0;
        int x = 0;
        char prev_char;
        for(x = 0; x < size; x++)
        {
    
    
        if((buffer[x] == '\n' || buffer[x] == '\t' || buffer[x] == ' ')&&
          (prev_char !=  '\n' && prev_char != '\t' && prev_char != ' ' && prev_char !="NULL"))
            word_count++;
        prev_char = buffer[x];
        }
    
    
        return word_count;
        }
    
    
    int lines_lines(char* buffer, int size) {
    
    
        int line_count = 0;
        int x = 0;
        for(x =0; x < size; x++)
        {
            if(buffer[x] == '\n')         
                line_count++;
        }
        return line_count;
    }
    
    
    int main (int argc, char* argv[]){
        
        int filesize = 0;
        int print_chars = 0; int lines_count = 0;
        int print_words = 0; int chars_count = 0;
        int print_lines = 0; int words_count = 0;
        int next_opt;
        char* filename = "/usr/share/dict/words";
        char filecontents[BUFFSIZE];
    
    
        const char* const short_opt = "hcwlf";
        const struct option long_opt[] = 
        {
            { "help", 0, NULL, 'h' },
            { "chars", 0, NULL, 'c' },
            { "words", 0, NULL, 'w' },
            { "lines",0, NULL, 'l' },
            { "file", 1, NULL, 'f' },
                { NULL, 0, NULL, 0 }
    };
    
    
    do {
        next_opt = getopt_long (argc, argv, short_opt,long_opt, NULL);
        switch (next_opt) {
             
            case 'h': 
                    printf ("-h --help Display this usage information.\n"
    
    
            "-c --chars Print number of characters in FILENAME.\n"
    
    
            "-w --words Print number of words in FILENAME.\n"
    
    
            "-l --lines Print number of lines in FILENAME.\n"
    
    
            "-f --file FILENAME Read from file.\n");)
                    break; 
                case 'c':
                    print_chars = 1;
                    break;
                case 'w':
                    print_words = 1;
                    break;
                case 'l': 
                    print_lines = 1;
                    break;
            case 'f': 
                    filename = optarg;
            filesize = read_file(filename, filecontents);
                    break;    
        }    
    
    
    }
        while (next_opt != -1);
    
    
        filesize = read_file(filename, filecontents);
        lines_count = lines_lines(filename, filecontents);
        chars_count = chars_chars(filename, filecontents);
        words_count = words_words(filename, filecontents);
    
    
        if(print_lines == 1)
            printf("%i\n", lines_count);
        if(print_chars == 1)
            printf("%i\n", chars_count);
        if(print_words == 1)
            printf("%i\n", words_count);
    
    
    }
    Any help with this would be greatly appreciated!

  2. #2
    Registered User
    Join Date
    Oct 2011
    Posts
    838
    Let your compiler help you, and enable warnings. For example, gcc -W -Wall adam-kemp.c -o binary:
    Code:
    adam-kemp.c: In function ‘read_file’:
    adam-kemp.c:19:9: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘char * (*)(int)’ [-Wformat]
    adam-kemp.c:27:13: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘char * (*)(int)’ [-Wformat]
    adam-kemp.c:32:13: warning: unknown conversion type character 0xa in format [-Wformat]
    adam-kemp.c:32:13: warning: too many arguments for format [-Wformat-extra-args]
    adam-kemp.c:38:13: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘char * (*)(int)’ [-Wformat]
    adam-kemp.c: In function ‘chars_chars’:
    adam-kemp.c:49:23: warning: unused parameter ‘buffer’ [-Wunused-parameter]
    adam-kemp.c: In function ‘words_words’:
    adam-kemp.c:68:81: warning: comparison between pointer and integer [enabled by default]
    adam-kemp.c:68:81: warning: comparison with string literal results in unspecified behavior [-Waddress]
    adam-kemp.c: In function ‘main’:
    adam-kemp.c:132:49: error: expected statement before ‘)’ token
    adam-kemp.c:155:5: warning: passing argument 2 of ‘lines_lines’ makes integer from pointer without a cast [enabled by default]
    adam-kemp.c:78:5: note: expected ‘int’ but argument is of type ‘char *’
    adam-kemp.c:156:5: warning: passing argument 2 of ‘chars_chars’ makes integer from pointer without a cast [enabled by default]
    adam-kemp.c:49:5: note: expected ‘int’ but argument is of type ‘char *’
    adam-kemp.c:157:5: warning: passing argument 2 of ‘words_words’ makes integer from pointer without a cast [enabled by default]
    adam-kemp.c:57:5: note: expected ‘int’ but argument is of type ‘char *’
    adam-kemp.c:94:9: warning: variable ‘filesize’ set but not used [-Wunused-but-set-variable]
    adam-kemp.c:168:1: warning: control reaches end of non-void function [-Wreturn-type]
    There are too many errors in your code for me to even know where to start. How about you first try to take care of the above errors and warnings?

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    2
    ... damn that's a lot of errors xD Thank you for posting. I'll give it a bash of fixing the errors/warnings and I'll post back when I have something a little less broken

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "Invalid chars", when I try to create a text file
    By frank1 in forum C Programming
    Replies: 7
    Last Post: 10-02-2012, 09:31 PM
  2. Replies: 1
    Last Post: 12-08-2007, 09:49 PM
  3. Replies: 10
    Last Post: 08-16-2007, 01:02 PM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 07:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 06:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21