By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,505 Members | 1,761 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,505 IT Pros & Developers. It's quick & easy.

Reading ints from a file

P: 7
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("\nProcessing 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("Threshold is %f.\n\n", *threshold);

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

/* 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);.

Nov 9 '08 #1
Share this Question
Share on Google+
3 Replies

Expert Mod 5K+
P: 8,916
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.
Nov 9 '08 #2

P: 7
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.
Nov 9 '08 #3

Expert 100+
P: 671
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 its a pointer, then indicate where it is pointing.
Nov 9 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.