开发者

Gets related problem

开发者 https://www.devze.com 2023-02-20 11:19 出处:网络
#include <stdio.h> int main () { FILE * pFile; int n; char name [开发者_StackOverflow100]; pFile = fopen (\"myfile.txt\", \"w\");
#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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号