yekcir
yekcir

Reputation: 1

Segmentation fault when trying to print string with a for loop in C

I am parsing a Wiktionary JSON dump with C in order to convert it into delimited format. My program works but when I print out my definition normally with the json_object_get_string function, I get brackets and quotation marks that I don't want to be shown. So I tried printing the string character by character, using a for loop and starting at the 4th character and stopping 3 before the termination character. But when I try doing this I keep getting a segmentation fault.

#include <stdio.h>
#include <json-c/json.h>
#include <string.h>
#include <stdbool.h>

int main(int argc, char **argv) {
    FILE *fp;

    struct json_object *sensestr;
    int glosseslength = 0;
    char *line;
    char *line_ii;
    char *prevword = NULL;
    int d = 0;
    int n_examples = 0;
    struct json_object *parsed_json;
    struct json_object *parsed_senses;
    struct json_object *pos;
    struct json_object *word;
    struct json_object *age;
    struct json_object *senses;
    struct json_object *sense;
    struct json_object *examples;
    struct json_object *example;

    struct json_object *glosses;
    struct json_object *tags;
    struct json_object *forg_text;
    struct json_object *eng_text;

    int list = 0;
    int wordlen;
    bool has_period = false;
    bool put_newline;

    int ma = 0;
    int mb = 0;

    char *alt;

    size_t n_friends;
    size_t len;
    size_t len_ii;

    size_t read_ii;
    size_t read;
    size_t n_glosses;
    size_t i;

    fp = fopen("kaik.json","r");

    while ((read = getline(&line, &len, fp)) != -1) {
        //printf("Retrieved line of length %zu:\n", read);
        //printf("%s", line);
        parsed_json = json_tokener_parse(line);
        json_object_object_get_ex(parsed_json, "senses", &senses);
        json_object_object_get_ex(parsed_json, "word", &word);
        json_object_object_get_ex(parsed_json, "pos", &pos);

        wordlen = strlen(json_object_get_string(word));

        if (prevword == NULL || strcmp(prevword, json_object_get_string(word)) != 0) {
            if (list != 0) {
                printf("\n");
            }

            printf("%i. %s\t", list, json_object_get_string(word));
        }

        list = list + 1;

        free(prevword);

        prevword = strdup(json_object_get_string(word));

        printf("[%s] ", json_object_get_string(pos));

        if (json_object_object_get_ex(parsed_json, "senses", &senses)) {
            for (int x = 0; x < json_object_array_length(senses); x++) {
                sense = json_object_array_get_idx(senses, x);

                json_object_object_get_ex(sense, "glosses", &glosses);

                if (json_object_get_string(glosses)) {
                    glosseslength = strlen(json_object_get_string(glosses));

                    for (int e = 3; e < glosseslength - 3; e++) {
                        printf("%c", json_object_get_string(glosses)[e]);        //this for loop causes a segmentation fault for some reason
                    }

                    printf("%s", json_object_get_string(glosses));   //this works
                }

                printf(" ");
            }
        }
    }

    fclose(fp);

    printf("\n");
}

Upvotes: 0

Views: 89

Answers (0)

Related Questions