Find Unique Characters in a File

前端 未结 22 2289
耶瑟儿~
耶瑟儿~ 2021-02-04 03:30

I have a file with 450,000+ rows of entries. Each entry is about 7 characters in length. What I want to know is the unique characters of this file.

For instance, if my f

22条回答
  •  感情败类
    2021-02-04 04:02

    A C solution. Admittedly it is not the fastest to code solution in the world. But since it is already coded and can be cut and pasted, I think it counts as "fast to implement" for the poster :) I didn't actually see any C solutions so I wanted to post one for the pure sadistic pleasure :)

    #include
    
    #define CHARSINSET 256
    #define FILENAME "location.txt"
    
    char buf[CHARSINSET + 1];
    
    char *getUniqueCharacters(int *charactersInFile) {
        int x;
        char *bufptr = buf;
        for (x = 0; x< CHARSINSET;x++) {
            if (charactersInFile[x] > 0)
                *bufptr++ = (char)x;
        }
        bufptr = '\0';
        return buf;
    }
    
    int main() {
        FILE *fp;
        char c;
        int *charactersInFile = calloc(sizeof(int), CHARSINSET);
        if (NULL == (fp = fopen(FILENAME, "rt"))) {
            printf ("File not found.\n");
            return 1;
        }
        while(1) {
            c = getc(fp);
            if (c == EOF) {
                break;
            }
            if (c != '\n' && c != '\r')
                charactersInFile[c]++;
        }
    
        fclose(fp);
        printf("Unique characters: {%s}\n", getUniqueCharacters(charactersInFile));
        return 0;
    }
    

提交回复
热议问题