1
#include <stdio.h>
#include <string.h>
#include "formatCheck.h"
int main()
    {
    char input[32];
    char format[32]
    printf("enter your format : ");
    fgets(input,sizeof(input),stdin);
    sscanf(input,"%s",format);
        //my problem
        //if user don't enter format it will exit.
         if()
            {
            return 0;
            }
    }

ユーザーが何も入力していないかどうかを確認するにはどうすればよいですか (Enter キーのみ)。英語でごめんなさい。ありがとう。

4

4 に答える 4

3

ユーザーが入力のみを押すと、input[0] contains \n

fgets(input,sizeof(input),stdin);
  if(input[0]=='\n') printf("empty string");
于 2013-11-08T05:44:40.007 に答える
0
/* fgets example */
#include <stdio.h>

int main()
{
   FILE * pFile;
   char mystring [100];

   pFile = fopen ("myfile.txt" , "r");
   if (pFile == NULL) perror ("Error opening file");
   else {
     if ( fgets (mystring , 100 , pFile) != NULL ) //Use this
       puts (mystring);
     fclose (pFile);
   }
   return 0;
}


/* fgets example 2 */
#include <stdio.h>

int main()
{
   FILE * pFile;
   char mystring [100];

   pFile = fopen ("myfile.txt" , "r");
   if (pFile == NULL) perror ("Error opening file");
   else {
     if ( fgets (mystring , 100 , pFile) && input[0]!='\n' ) //Use this
       puts (mystring);
     fclose (pFile);
   }
   return 0;
}

参考:http ://www.cplusplus.com/reference/cstdio/fgets/

于 2013-11-08T05:44:21.357 に答える