fgets returns invalid parameter error
Hello all,
I am reading from one file, formatting the data and writing it to a different file in the following way:
Code:
char input[256];
FILE *fraw; -- initialised earlier.
while (NULL != fgets(input,sizeof(input),fraw)
{
if (5 == sscanf(input, "** formatting selection ** ))
// process data
else
// report error
}
This works for a while but often after 62 lines fgets only reads part of the data and reports and invalid parameter error. Sometimes it happens in lines other than the 62nd. The line in question is fine and is in the middle of a load of output generated by the program (all in exactly the same way).
Can anyone spot what I have done wrong here?
Re: fgets returns invalid parameter error
Re: fgets returns invalid parameter error
Good spot, I have corrected my post.
It is a FILE* in the code so that would not be the issue - although my lack of attention to detail might be!
Re: fgets returns invalid parameter error
fgets is looking for a pointer..
while (NULL != fgets( input, sizeof(input), fraw)
should be
while ( NULL != fgets( &input, sizeof(char * 256), fraw)
I believe.
Re: fgets returns invalid parameter error
Thanks for your help but that is not it :-(
Re: fgets returns invalid parameter error
1. "input" is correct , not "&input"
2. what is the value of input when the error occurs ?
3. Are all lines less than 256 characters ?
Re: fgets returns invalid parameter error
The value in the file is something like:
2855429698 78564831321 5886543215564 6546845636 78794565465
input has the first 3 values correct and then the first 4 digits of the 4th.
All lines are less than 256 although I tried changing the buffer to 5096 to see if that helped and it did not.
Re: fgets returns invalid parameter error
What does the format of the sscanf look like ? And the data types
of the variables ?
Re: fgets returns invalid parameter error
sscanf looks like
sscanf(input,"%lu %lu %lu %lu %lu", &d1, &d2, &d3, &d4, &d5)
The variables are long ints. Also, the output of the first 61 lines is valid.
Re: fgets returns invalid parameter error
it should probably be "%ld" for long int, but that should not be a problem.
Just verify that sizeof(long int) == 8
Does this code produce the correct result ?
Code:
#include <stdio.h>
#include <string.h>
int main()
{
char input[256];
long int d1 , d2 , d3 , d4 , d5;
strcpy(input , "2855429698 78564831321 5886543215564 6546845636 78794565465");
int n = sscanf(input,"%ld %ld %ld %ld %ld", &d1, &d2, &d3, &d4, &d5);
printf("n = %d\n",n);
printf("d1 = %ld\n",d1);
printf("d2 = %ld\n",d2);
printf("d3 = %ld\n",d3);
printf("d4 = %ld\n",d4);
printf("d5 = %ld\n",d5);
printf("sizeof(long int)= %d\n",sizeof(long int));
}
Re: fgets returns invalid parameter error
That worked fine.
I actually tried embedding it next to my work code and it worked every time through the loop - although the loop broke at 55 rather than 62 this time (there were 66 lines in the input file).
Anyone got any ideas????? I am stumped!
Re: fgets returns invalid parameter error
Quote:
Originally Posted by
SteveTaylor
Anyone got any ideas?????
Yes, you have a bug in your code that is random, and is more than likely, corrupting memory.
You haven't posted anything substantial to help solve your problem any further. We have no idea what your real program looks like or what your file really consists of.
Regards,
Paul McKenzie
Re: fgets returns invalid parameter error
Okay I have tried some more changes and the following is causing the same issue. The input file has about 90 entries and this stopped working on the 62nd:
Code:
while (5 == fscanf(fraw, "%lu %lu %lu %lu %lu", &D1, &D2, &D3, &D4, &D5))
{
linesWritten++;
fprintf(ffor,"%lu, %lu, %lu, %lu, %lu, %lu\n",
D1,
D2,
(D1- D2),
(D3- D2),
(D4- D1),
(D5- D1));
}
This is the input file format:
2267924448 242996103589964 242996103591341 242996103591477 242996103609081
2267927216 242996103663243 242996103664688 242996103664798 242996103670621
2267946020 242996104269242 242996104271350 242996104271494 242996104291682
The last line is the one that failed, all of the lines are formatted the same as these three.
Re: fgets returns invalid parameter error
Quote:
Originally Posted by
SteveTaylor
Okay I have tried some more changes and the following is causing the same issue. The input file has about 90 entries and this stopped working on the 62nd:
Again, we have no idea if the reason it stops is because you previously corrupted memory or not.
You are basically showing us static code, and nothing is wrong with it as you have written. What is wrong is the running of your entire program -- it could be reading from the file incorrectly, you could be allocating too little memory, you could be reading in something that exceeds the bufffer size, etc.
Also, what is magical about entry number 62? Is it different in some way then any of the other entries? What if you made this entry first instead of number 62? If there is no difference, then the problem is elsewhere in your running code, not the code you posted.
Regards,
Paul McKenzie
Re: fgets returns invalid parameter error
I see what you mean but the only way to show the full inter-related code would be to post the entire file which I am not allowed to do.
Ah well, thanks to all who tried to help.