-3

I am trying to read in a text file, which contains integers, and place them into an array. I will be sorting them once I have the array created. However, I trying to make sure things work as I implement them but I am getting a segmentation fault when I attempt to open a file. I do not understand why.

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <ctype.h>

#define SIZE_T_MAX 16777216

/****** FORWARD DECLARATIONS ******/
void die(const char *message) {
  if(errno) {
    perror(message);
  } else {
    printf("ERROR: %s\n", message);
  }

  exit(1);
}

// ARRAY
typedef struct int_array {
  int *array;
  size_t length;
  size_t capacity;
} int_array;

void int_array_init(int_array *array);
void int_array_free(int_array *array);
void int_array_add(int_array *array, int value);
/****** END OF DECLARATIONS  ******/
//
//
/********** MAIN PROGRAM **********/
int main(char argc, char *argv[]) {
  // VARIABLES
  FILE *data;
  int_array numbers;
  int_array_init(&numbers);
  int i = 0;
  char value[10];

  // LOAD FILES
  printf("Loading files...\n");

  data = fopen("list0.txt", "r");
  if(data == NULL) die("Failed to load file.");

  while(!feof(data) && !ferror(data)) {
    fscanf(data, "%s", value);
    int_array_add(&numbers, atoi(value));
    printf("%d", numbers.array[i]);
    i++;
  }

  // CLEAN UP
  int_array_free(&numbers);
  fclose(data);

  return 0;
}
/*********** END OF MAIN **********/
//
//
/******* ADDITIONAL METHODS *******/
void int_array_init(int_array *array) {
  array->array = NULL;
  array->length = 0;
  array->capacity = 0;
}

void int_array_free(int_array *array) {
  free(array->array);
  array->array = NULL;
  array->length = 0;
  array->capacity = 0;
}

void int_array_add(int_array *array, int value) {
  if (array->length == array-> capacity)
  {
    int new_capacity;

    if (array->capacity == 0) new_capacity = 10;
    else new_capacity = array->capacity * 2;

    if (new_capacity > array->capacity && new_capacity < SIZE_T_MAX / sizeof(int)) {
      int *new_array = realloc(array->array, new_capacity * sizeof(int));
      if (new_array != NULL) {
        array->array = new_array;
        array->capacity = new_capacity;
      } else {
        die("Out of Memory");
      }
    } else {
      die("Overflow.");
    }
  }

  array->array[array->length] = value;
  array->length++;
}
  • 1
    sure it's at `fopen()` ? - looks ok to me. Only obvious issue I see is `argv` whould be a `char**` or `char *argv[]`. – John3136 Feb 04 '14 at 02:08
  • Ewps, yeah I'll change that part.. overlooked. At any rate, I assumed it was at this position. As my code runs fine up until this point. I've tried commenting out all the array information just to narrow it down and it is not wanting to execute that "FILE *data" line no matter what I do. – user2778322 Feb 04 '14 at 02:13
  • Is this your actual code? There is no array info before `FILE *data` (which doesn't actually "do" anything). – John3136 Feb 04 '14 at 02:15
  • Try to declare the FILE pointer variable at the beginning of the main function. – Abend Feb 04 '14 at 02:17
  • Sorry, I think I may have mislead. I don't think the FILE *data is actually the cause of it, but rather something I am doing else where with memory allocation? I am still new to this C language, but it seems to me something is not allocating memory correctly. – user2778322 Feb 04 '14 at 02:21
  • In your function int_array_add you have `if (array->length == array-> capacity)` which on the first addition should evaluate to 0 == 0 and trouble begins. – gmorrow Feb 04 '14 at 02:29
  • Use a debugger. Use Valgrind. – bmargulies Feb 04 '14 at 02:49
  • You're reading a string into `value`, but you've never pointed `value` to any storage. Uninitialized pointers are lethal to code. See also [`while (!feof(file))` is always wrong](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong)! – Jonathan Leffler Feb 04 '14 at 03:17
  • Thanks for the information! I literally just noticed the while loop issue. – user2778322 Feb 04 '14 at 03:22
  • at `array_init`: `array->capacity = 10;` wrong. should be `array->capacity = 0;` – BLUEPIXY Feb 04 '14 at 09:29

1 Answers1

0

I have corrected and fixed my code. The problem was in the while loop being infinite and corrected with:

while(!feof(data) && !ferror(data))