Well array overrun looks a very strong candidate to me, given your latest description.
> I read data off an input file into the array.
You don't have any
while (!feof(fp)) loops do you?
Because this is almost certainly wrong, and frequently leads to an extra element being 'read(*)' from the file. If you sized your arrays to exactly match the file, then you would end up with an array overrun.
(*) Since it was almost at end of file, what is typically seen is the last data in the file being duplicated.
Code:
$ gcc foo.c
$ ./a.out
#include <stdio.h>
int main(void)
{
FILE *fp = fopen("foo.c","r");
char buff[BUFSIZ];
while ( !feof(fp) ) {
fgets(buff,BUFSIZ,fp); fputs(buff,stdout);
}
rewind(fp);
while ( fgets(buff,BUFSIZ,fp) != NULL ) {
fputs(buff,stdout);
}
fclose(fp);
return 0;
}
/* the last line */
/* the last line */
#include <stdio.h>
int main(void)
{
FILE *fp = fopen("foo.c","r");
char buff[BUFSIZ];
while ( !feof(fp) ) {
fgets(buff,BUFSIZ,fp); fputs(buff,stdout);
}
rewind(fp);
while ( fgets(buff,BUFSIZ,fp) != NULL ) {
fputs(buff,stdout);
}
fclose(fp);
return 0;
}
/* the last line */
As you can see, the broken feof() loop causes the last line of the file to be output twice. The very last fgets() call read no data at all, but left the previous buffer unmodified. Unless you check the fgets() itself for success, you could well end up processing the same buffer again.