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.
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.
Comment