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:

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


Recommended Answers

All 3 Replies

>int *x;
>fscanf(fin, "%i", x);
Where does x point?

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.

>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:

int x;

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:

char buffer[80];

fscanf ( in, "%79s", buffer );

Array names are converted to a pointer to the first element, so adding the address-of operator would be incorrect.

char *buffer = malloc ( 80 );

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.