Reading ints from a file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jordanbondo
    New Member
    • Aug 2008
    • 7

    Reading ints from a file

    I have no clue why this isn't working. The top part that reads into size, shortTime, longTime, and threshold work fine. Down below where I try to do the exact same thing and read into x, i get segmentation fault every single time. It seems to me that everything is exactly the same. Reading a value from the file into an int*. Does anyone see what is wrong here?

    Don't worry about the array and other stuff. I'm going to be putting the values into an array and returning that. Right now I'm just worried about why I can get the values for size, shortTime, longTime, and threshold, but I can't get anything else after that.

    I've looked around all over and found many places that talk about this, but it appears to me that I've done this correctly, so if someone could kindly point out what I'm missing I'd appreciate it.

    Here's the code:

    int* readData(int* size, int* shortTime, int* longTime, double* threshold, FILE* fin)
    {
    fscanf(fin, "%i", size);
    fscanf(fin, "%i", shortTime);
    fscanf(fin, "%i", longTime);
    fscanf(fin, "%lf", threshold);

    printf("\nProce ssing Data\n");
    printf("There are %i intervals in the data set.\n", *size);
    printf("Short-time interval is %i.\n", *shortTime);
    printf("Long-time interval is %i.\n", *longTime);
    printf("Thresho ld is %f.\n\n", *threshold);

    /* Allocate memory for the array & make sure it worked */
    int *array = (int*)calloc(*s ize, sizeof(int));
    if(array == NULL)
    {
    printf("Unable to allocate memory for array.");
    exit(0);
    }

    /* Read data from the file into the array */
    int ndx;
    int *x;
    for(ndx = 0; ndx < *size; ndx++)
    {
    printf("Get data for array[%i]...\n", ndx);
    fscanf(fin, "%i", x);
    printf("X is %i...\n", x);
    }
    for(ndx = 0; ndx < *size; ndx++)
    {
    printf("Array[%i] is %i\n", ndx, *array);
    array += 1;
    }
    return array;
    }

    Here's my input file:

    7 2 5 1.5
    1 2 1 1 1 5 4

    When I print size, shortTime, longTime, and threshold, i get out 7, 2, 5, and 1.5, just like i'm supposed to. I know the problem lies somewhere with the fscanf(fin, "%i", x), but I don't know why. If I take it out the loop goes on just fine and I get 7 lines of printf("Get data for array[%i]...\n", ndx);.

    Thanks.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    You allocate array but then read from the file to the uninitialised pointer x and then destroy the value of array in your printf loop before returning the, now invalid, value.

    Comment

    • jordanbondo
      New Member
      • Aug 2008
      • 7

      #3
      I'm aware that the part with the array is wrong. I wasn't asking about that. I understand that part isn't right. I'm asking why it works when i do

      fscanf(fin, "%i", size);

      I don't have a problem, but when i do

      fscanf(fin, "%i", x);

      It doesn't work. I tried declaring x as int* x AND as int x and I have the same problem either way.

      Comment

      • oler1s
        Recognized Expert Contributor
        • Aug 2007
        • 671

        #4
        Not only did you did not see what Banfa was trying to point out, but further comments from you:
        “I tried declaring x as int* x AND as int x and I have the same problem either way.”
        Suggest gaps in your knowledge. Do you know what int* x is? Do you know how it differs from int x? If you tell me it’s a pointer, then indicate where it is pointing.

        Comment

        Working...