943,598 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 660
  • C RSS
Nov 8th, 2008
0

Reading ints from a file

Expand Post »
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.

Here's the code:
  1. int* readData(int* size, int* shortTime, int* longTime, double* threshold, FILE* fin)
  2. {
  3. fscanf(fin, "%i", size);
  4. fscanf(fin, "%i", shortTime);
  5. fscanf(fin, "%i", longTime);
  6. fscanf(fin, "%lf", threshold);
  7.  
  8. printf("\nProcessing Data\n");
  9. printf("There are %i intervals in the data set.\n", *size);
  10. printf("Short-time interval is %i.\n", *shortTime);
  11. printf("Long-time interval is %i.\n", *longTime);
  12. printf("Threshold is %f.\n\n", *threshold);
  13.  
  14. /* Allocate memory for the array & make sure it worked */
  15. int *array = (int*)calloc(*size, sizeof(int));
  16. if(array == NULL)
  17. {
  18. printf("Unable to allocate memory for array.");
  19. exit(0);
  20. }
  21.  
  22. /* Read data from the file into the array */
  23. int ndx;
  24. int *x;
  25. for(ndx = 0; ndx < *size; ndx++)
  26. {
  27. printf("Get data for array[%i]...\n", ndx);
  28. fscanf(fin, "%i", x);
  29. printf("X is %i...\n", x);
  30. }
  31. for(ndx = 0; ndx < *size; ndx++)
  32. {
  33. printf("Array[%i] is %i\n", ndx, *array);
  34. array += 1;
  35. }
  36. return array;
  37. }

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.
Last edited by bondo; Nov 8th, 2008 at 9:38 pm. Reason: Needed to add stuff.
Similar Threads
Reputation Points: 11
Solved Threads: 0
Light Poster
bondo is offline Offline
43 posts
since Apr 2007
Nov 9th, 2008
1

Re: Reading ints from a file

>int *x;
>fscanf(fin, "%i", x);
Where does x point?
Last edited by Narue; Nov 9th, 2008 at 10:15 am.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 12th, 2008
0

Re: Reading ints from a file

Oops, that was an accident. I didn't know if x needed to be a pointer for some reason. I was pretty sure it didn't, but since it wasn't working anyways I thought I'd try it. That really should have just been:

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

I eventually wound up getting this to work. The final code doesn't look much different, so I'm not sure if I had a type-o or something stupid, but I get it working. I don't really know HOW I got it working. Not sure what I changed, but in the end I got it.
Reputation Points: 11
Solved Threads: 0
Light Poster
bondo is offline Offline
43 posts
since Apr 2007
Nov 12th, 2008
1

Re: Reading ints from a file

>I didn't know if x needed to be a pointer for some reason.
fscanf expects a pointer, but the pointer has to point somewhere. The usual pattern is to create an object and then pass the address of that object to fscanf:
  1. int x;
  2.  
  3. fscanf ( in, "%d", &x );
That way x exists and fscanf gets a pointer so that it can modify x properly. Another pattern is if the object is already a pointer for some reason, but still points to valid memory:
  1. char buffer[80];
  2.  
  3. fscanf ( in, "%79s", buffer );
Array names are converted to a pointer to the first element, so adding the address-of operator would be incorrect.
  1. char *buffer = malloc ( 80 );
  2.  
  3. fscanf ( in, "%79s", buffer );
In this case buffer is an explicit pointer, but it points to memory owned by the program's process. All is well.

If you deviate from these patterns, you're probably doing something wrong.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: pointers to function
Next Thread in C Forum Timeline: redirect output to memory





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC