Gets related problem
#include <stdio.h>
int main ()
{
FILE * pFile;
int n;
char name [开发者_StackOverflow100];
pFile = fopen ("myfile.txt", "w");
for (n = 0; n < 3; n++)
{
puts ("please, enter a name: ");
gets (name);
fprintf (pFile, "Name %d [%-10.10s]\n", n, name);
}
fclose (pFile);
return 0;
}
this code gives me a warning in gcc saying 'gets is a dangerous function to use'...is there a workaround for it?
Yes it is fgets
. Replace your call to gets
with:
fgets(name, 100, stdin)
For more details see the docs - the two are not exactly the same.
精彩评论