Skip to content

Instantly share code, notes, and snippets.

@sats17
Created December 31, 2024 15:29
Show Gist options
  • Select an option

  • Save sats17/c9c0ba38d91440ab0091f587b9d5b540 to your computer and use it in GitHub Desktop.

Select an option

Save sats17/c9c0ba38d91440ab0091f587b9d5b540 to your computer and use it in GitHub Desktop.

Revisions

  1. sats17 created this gist Dec 31, 2024.
    1 change: 1 addition & 0 deletions dictonary.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    apple,a sweet fruit
    115 changes: 115 additions & 0 deletions word_dict_reader.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,115 @@
    #include <stdio.h>
    #include <stdbool.h>
    #include <ctype.h>
    #include <stdlib.h>
    #include <string.h>

    bool is_string_matching(char *str1, char *str2) {
    int i = 0;
    while (str1[i] != '\0' && str2[i] != '\0') {
    if (tolower((unsigned char)str1[i]) != tolower((unsigned char)str2[i])) {
    return false;
    }
    i++;
    }
    if (str1[i] != '\0' || str2[i] != '\0') {
    return false;
    }
    return true;
    }


    char* read_data_from_file(char *input) {
    FILE *file;

    file = fopen("dictonary.txt", "r");
    if (file == NULL) {
    printf("File not found\n");
    return NULL;
    }
    char ch;
    char buffer[256];
    int index = 0;

    while ((ch = fgetc(file)) != EOF) {
    if (ch == ',') {
    buffer[index] = '\0';
    if (is_string_matching(buffer, input)) {
    int output_index = 0;
    char *output = (char*)malloc(256 * sizeof(char));
    while((ch = fgetc(file)) != '\n' && ch != EOF) {
    output[output_index++] = ch;
    }
    output[output_index] = '\0';
    fclose(file);
    return output;
    } else {
    while((ch = fgetc(file)) != '\n' && ch != EOF);
    }
    index = 0;
    } else {
    buffer[index++] = ch;
    }
    }
    fclose(file);
    return NULL;
    }

    bool add_data_to_file(char *word, char* meaning) {
    FILE *file;
    file = fopen("dictonary.txt", "a");
    if (file == NULL) {
    printf("File not found\n");
    return false;
    }
    fprintf(file, "\n%s,%s", word, meaning);
    fclose(file);
    return true;
    }


    int main(int argc, char *argv[]) {
    if (argc < 2) {
    printf("Usage: %s <command> [arguments]\n", argv[0]);
    printf("Commands:\n");
    printf(" add <word> <meaning> Add a new word and its meaning to the dictionary\n");
    printf(" read <word> Read the meaning of a word from the dictionary\n");
    return 1;
    }

    if (strcmp(argv[1], "add") == 0) {
    if (argc != 4) {
    printf("Usage: %s add <word> <meaning>\n", argv[0]);
    return 1;
    }
    printf("Adding word...\n");
    printf("Word: %s\n", argv[2]);
    printf("Meaning: %s\n", argv[3]);
    if (add_data_to_file(argv[2], argv[3])) {
    printf("Word added successfully\n");
    } else {
    printf("Failed to add word\n");
    }
    } else if (strcmp(argv[1], "read") == 0) {
    if (argc != 3) {
    printf("Usage: %s read <word>\n", argv[0]);
    return 1;
    }
    char *meaning = read_data_from_file(argv[2]);
    if (meaning != NULL) {
    printf("Meaning: %s\n", meaning);
    free(meaning); // Remember to free the allocated memory
    } else {
    printf("Word not found\n");
    }
    } else {
    printf("Unknown command: %s\n", argv[1]);
    printf("Usage: %s <command> [arguments]\n", argv[0]);
    printf("Commands:\n");
    printf(" add <word> <meaning> Add a new word and its meaning to the dictionary\n");
    printf(" read <word> Read the meaning of a word from the dictionary\n");
    return 1;
    }

    return 0;
    }