| | |
Reading ints from a file
Thread Solved
![]() |
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:
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.
Here's the code:
C Syntax (Toggle Plain Text)
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."); 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.
Last edited by bondo; Nov 8th, 2008 at 9:38 pm. Reason: Needed to add stuff.
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.
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:
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:
Array names are converted to a pointer to the first element, so adding the address-of operator would be incorrect.
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.
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:
c Syntax (Toggle Plain Text)
int x; fscanf ( in, "%d", &x );
c Syntax (Toggle Plain Text)
char buffer[80]; fscanf ( in, "%79s", buffer );
c Syntax (Toggle Plain Text)
char *buffer = malloc ( 80 ); fscanf ( in, "%79s", buffer );
If you deviate from these patterns, you're probably doing something wrong.
I'm here to prove you wrong.
![]() |
Similar Threads
- fstream Tutorial (C++)
- Reading data file in C (C)
- reading ints from a binary file (C)
- Nugget? reading different int from a file (C++)
- What errors can occur while reading a file? (C)
- Reading data into files (C++)
- Reading from external data file (C++)
- reading input ... quick C++ question ... (C++)
- Text File Input and manipulation (C++)
Other Threads in the C Forum
- Previous Thread: pointers to function
- Next Thread: redirect output to memory
| Thread Tools | Search this Thread |
* ansi array asterisks binarysearch calculate centimeter changingto char character cm convert copyanyfile copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createprocess() database feet fflush fgets file floatingpointvalidation fork forloop givemetehcodez global grade gtkwinlinux hacking histogram homework i/o inches infiniteloop input interest intmain() iso kernel keyboard kilometer km linked linkedlist linux locate looping loopinsideloop. lowest match meter microsoft mqqueue number oddnumber odf open opendocumentformat openwebfoundation owf pattern pdf performance posix power probleminc process program programming pyramidusingturboccodes radix read recv recvblocked research reversing scanf segmentationfault sequential single socket socketprograming socketprogramming standard string suggestions systemcall threads turboc unix urboc user variable voidmain() wab whythiscodecausesegmentationfault win32api windowsapi






