|
-
April 4th, 2011, 09:23 AM
#1
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?
Last edited by SteveTaylor; April 4th, 2011 at 09:47 AM.
-
April 4th, 2011, 09:47 AM
#2
Re: fgets returns invalid parameter error
Get Microsoft Visual C++ Express here or CodeBlocks here.
Get STLFilt here to radically improve error messages when using the STL.
Get these two can't live without C++ libraries, BOOST here and Loki here.
Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
Always use [code] code tags [/code] to make code legible and preserve indentation.
Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.
-
April 4th, 2011, 09:49 AM
#3
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!
-
April 4th, 2011, 09:59 AM
#4
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.
-
April 4th, 2011, 10:06 AM
#5
Re: fgets returns invalid parameter error
Thanks for your help but that is not it :-(
-
April 4th, 2011, 10:35 AM
#6
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 ?
-
April 4th, 2011, 10:38 AM
#7
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.
-
April 4th, 2011, 10:56 AM
#8
Re: fgets returns invalid parameter error
What does the format of the sscanf look like ? And the data types
of the variables ?
-
April 4th, 2011, 11:00 AM
#9
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.
-
April 4th, 2011, 11:25 AM
#10
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));
}
-
April 5th, 2011, 03:18 AM
#11
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!
-
April 5th, 2011, 03:39 AM
#12
Re: fgets returns invalid parameter error
 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
Last edited by Paul McKenzie; April 5th, 2011 at 03:44 AM.
-
April 5th, 2011, 03:46 AM
#13
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.
Last edited by SteveTaylor; April 5th, 2011 at 03:49 AM.
-
April 5th, 2011, 10:09 AM
#14
Re: fgets returns invalid parameter error
 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
Last edited by Paul McKenzie; April 5th, 2011 at 10:16 AM.
-
April 6th, 2011, 04:00 AM
#15
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|