sed/shell scripting - add line if needed and not allready there

I am writing a shell script that checks all .c files to see if they use fprintf or printf. If a file does, then the line #include <stdio.h> is added to the top of the file, unless it's already there.

This is what I've got:

#!/bin/sh
egrep -l f?printf *.c | while read file;
do sed -i '1i\
#include <stdio.h>' $file; 
done

The problem is that it doesn't check to see if <stio.h> is allready there, so I've been thinking about using

egrep -l f?printf *.c | grep -L '#include <stdio.h>

but this doesn't work because it anonimizes the files when I use the pipe.

Might be with a bit modification to your script is enough :

#!/bin/sh
egrep -l f?printf *.c | while read file;
do 
grep "#include <stdio.h>" $file > dev/null
if [ $? -ne 0 ]; then
sed -i '1i\
#include <stdio.h>' $file; 
fi
done

thanks