NO

Author Topic: The getc (gets) character command  (Read 4352 times)

ljay0778

  • Guest
The getc (gets) character command
« on: February 27, 2015, 08:33:32 AM »
Cant compile the program in Pelles C only in DEV+  It reports error with "gets"  wants to call it _getch but it still wont run. then it wasnts a Go switch but does not show how to use it. how would this simple program be worded so it runs? in Pelles C _Thank you.

 // demonstrates opening files//

#include<conio.h>
#include<stdio.h>
   
   
    int   main()
   
    {
       
      
         FILE *fp;   // pointer to point to our file we want to open//
         char ch, filename[40], mode[4];   // initialize variables ch, filenane and mode//
      
           
         while(1)  // do forever//
      {
          // input the file name we want opened//

         printf("\n Enter the file name to open:  ");
         gets(filename);
         printf("\nEnter the mode to use on the file (Max 3 cgaracters):  ");// r,w,a r+,w+ a+//
         gets(mode);

         // try to open the file//

         if ( (fp = fopen( filename, mode)) != NULL )
            {         
         
               printf("\n Successful opening %s in mode %s.\n",
            filename, mode);      
         
            fclose(fp);

            puts("Enter x to exit or any other key to continue.");
            if ((ch = getchar()) == 'x')
            break;
               else
                  continue;

                     }

                  else
               {
             fprintf( stderr,"\nError opening file %s in mode %s.", filename, mode);
            puts("\nEnter x to exit or any othr key to try again: ");
            if ((ch = getchar()) == 'x')
            break;
               else
                  continue;

                           }         
                        }

                  }


Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: The getc (gets) character command
« Reply #1 on: February 28, 2015, 01:50:43 AM »
Welcome to the wonderful world of C11.

gets() is considered deprecated and should be replaced with gets_s() or better, fgets() instead...

Ralf

PS: You might try and reconsider your source formatting style, you are programming in C, not Python...