odare
odare

Reputation: 19

Seg fault from within my print function after reading in a file in c

I have an unknown segfault within my print function when I call it in main and I can't see what the obvious fix is. I have put printf's throughout the program and it doesn't print 'here4' making me think it's due to my print function, or when I call it in main.

I want to read a dictionary file into an array of strings.

Here is a snippet of the code:

Any ideas would be greatly appreciated, thanks.

#define PRIME 1009

void fileRead(int argc, char **argv)
    void printTable(int arrayLength, char **table); 

int main(int argc, char **argv)
{
    char **table;
    FILE *fp;
    int i, arrayLength = PRIME;

    /* Initial memory allocation */
    table = (char**)malloc(PRIME*sizeof(char));

    fileRead(argc, argv);
    printf("here3\n");
    for(i = 0; i < arrayLength; i++) {
        printTable(arrayLength,table); 
    }
    printf("here4\n");
    return 0;

}

void fileRead(int argc, char **argv)
{
    FILE *fp;
    char *word;
    int arrayLength = PRIME;

    word = calloc(MAXCHAR, sizeof(char));

    fp = fopen (argv[1], "r");
    printf("here1\n");
    /*read in grid and move along a cell each time */
    while (fscanf(fp, "%s", word)!= EOF) {
        if (argc != (2)) {
            fprintf(stderr, "Cannot open file, %s\n Try again e.g. %s dictionary.txt\n" , argv[1], argv[0]);
        }
        if(fp == NULL) {
            fprintf(stderr, "Cannot open file, %s\n Try again e.g. %s dictionary.txt\n" , argv[1], argv[0]);
            return;
        }
        if (fp == NULL) {
            fprintf(stderr, "Error opening file, try file name dictionary.txt\n");
            exit(EXIT_FAILURE);
        }
    } 
    printf("here2\n");
    fclose(fp);
    return;
}

void printTable(int arrayLength, char **table) 
{
    int i; 
    for(i = 0; i < arrayLength; i++) {  
        printf("%s\n", table[i]);
    }
    printf("\n");
}

Upvotes: 0

Views: 48

Answers (2)

BLUEPIXY
BLUEPIXY

Reputation: 40155

try this

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define PRIME 1009
#define MAXCHAR 256

char **fileRead(FILE *fp, int *len);
void printTable(int arrayLength, char **table); 

int main(int argc, char **argv){
    if (argc != 2) {
        fprintf(stderr, "Need dictionary file argument.\nTry again e.g. %s dictionary.txt\n" , argv[0]);
        exit(EXIT_FAILURE);
    }

    FILE *fp = fopen (argv[1], "r");
    if(fp == NULL) {
        fprintf(stderr, "Cannot open file, %s\nTry again e.g. %s dictionary.txt\n" , argv[1], argv[0]);
        exit(EXIT_FAILURE);
    }

    int arrayLength = PRIME;
    char **table = fileRead(fp, &arrayLength);//fclose(fp) inside this

    printTable(arrayLength, table);

    for(int i = 0; i < arrayLength; ++i)
        free(table[i]);
    free(table);

    return 0;
}

char **fileRead(FILE *fp, int *len){
    char *word = calloc(MAXCHAR, sizeof(char));
    char **table = malloc(*len * sizeof(char*));
    int i = 0;

    while (i < *len && fscanf(fp, "%s", word) != EOF){
        table[i] = malloc(strlen(word)+1);
        strcpy(table[i++], word);
    }
    fclose(fp);
    *len = i;
    free(word);

    return table;
}

void printTable(int arrayLength, char **table){
    int i; 
    for(i = 0; i < arrayLength; i++) {  
        printf("%s\n", table[i]);
    }
    printf("\n");
}

Upvotes: 1

user5329483
user5329483

Reputation: 1272

Let me summarize your code:

  • you allocate uninitialized memory for table
  • You call a function fileRead():
    • Allocate some memory for word
    • read the file
    • Do nothing with the data read.
    • fileRead() does nothing useful: It does not return anything, it doesn't touch table, is vulnerable to a buffer overflow of word and leaves the memory leak of word behind.
  • And then you printf the unchanged and uninitialized content of table

Upvotes: 1

Related Questions