0

I've been having problems trying to pass numbers from a file to structure.

The file I'm trying to read has this format:

1002 January 287.4
1002 February 22.3
1002 March 51.4

This is my code thus far.

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

struct RainData{

    int stationID;
    float yearTotal;

};

int main(){

    int n, i, x;
    struct RainData *dataPtr;

    FILE *fptr;
    fptr = fopen("rain.dat","r");

    if (fptr == NULL){

        printf("Error in opening file\n");

        return 1;

    }

    fscanf(fptr, "%d", &n);
    dataPtr = (struct RainData *) malloc(n * sizeof(struct RainData));

    for (i = 0; i < n; i++){

        for (x = 0; x < 12; x++)

            fscanf(fptr, "%d" "%f", &(*dataPtr).stationID[i], &(*dataPtr).yearTotal[i]);

    }

    fclose(fptr);
    free(dataPtr);

    return 0;

}

When I run this, I get a segmentation fault error. I'm pretty sure my mistake is with the malloc, but I can't for the life of me figure it out. I'm not sure how to make the variables into arrays by using malloc. I'm also aware of the missing "%s" in the fscanf. The question does not use the months, so I would like to avoid if possible to assign a useless variable just to contain that.

Thanks in advance!

6
  • 2
    Your references to the data you're reading aren't correct. It looks like you allocated an array of struct RainData, so you should have, for example, &(dataPtr[i].stationID) rather than &(*dataPtr).stationID[i]. The latter is treating stationID like an array inside the structure, which it isn't. And what's the purpose of the for ( x =... loop? x isn't referenced inside the loop, so you're just reading into the same variables for each loop iteration.
    – lurker
    Sep 3, 2013 at 23:29
  • 1
    ... and is there a single int value at the beginning of the file that dictates the row-count? I ask because there isn't one in your sample data (according to that data coupled with your logic, you should be reading 1002 rows, which I'm confident isn't correct).
    – WhozCraig
    Sep 3, 2013 at 23:36
  • @mbratch Sweet, thanks! There's more to this actually, like how we need to add up the total of 12 months for 5 different stations. The later parts are incomplete. Sep 3, 2013 at 23:40
  • 1
    Pfft. I'd use none-of-this and fire up Excel with a whitespace delimiter import if that were the case. =P
    – WhozCraig
    Sep 3, 2013 at 23:43
  • I would agree that this is perhaps not the most optimal solution to sort the data but marks are sadly marks :/ Sep 3, 2013 at 23:45

1 Answer 1

0

There are couple of issues. First, as @mbratch pointed out, dataPtr is an array of struct, so you must use hte correct subscript to refer to that and then use the "." to refer to its member. Second, your file contains rows in the format of "%d %s %f", so you need to update the fscanf as well. Third, you need to add the total number of rows at the top since that is wehre you are reading the value of n from. Lastly, not sure, why you have x going from 0 to 12 -- you can delete that.

Try this main code:

int n, i, x;
struct RainData *dataPtr;
FILE *fptr;
char temp_str[100];

fptr = fopen("rain.dat","r");
if (fptr == NULL){
    printf("Error in opening file\n");
    return 1;
}

fscanf(fptr, "%d", &n);
dataPtr = (struct RainData *) malloc(n * sizeof(struct RainData));
printf("n is %d\n", n);

for (i = 0; i < n; i++){
    fscanf(fptr, "%d %s %f", &dataPtr[i].stationID, temp_str, &dataPtr[i].yearTotal);
}
for (i = 0; i < n; i++){
    printf("%d \t %f\n", dataPtr[i].stationID, dataPtr[i].yearTotal);
}

fclose(fptr);
free(dataPtr);
return 0;

With this rain.dat file:

3
1002 January 287.4
1002 February 22.3
1002 March 51.4
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.