Determining Char String length in C - if user enters string content

I know that in C you can declare a string and number of characters as shown below,

char mystring[50];

with '50' - the number of characters.

However, what is the correct procedure if the user enters the contents of the string (via scanf ("% s", mystring);)? I leave him like

char mystring[0];

leaving it as '0', since I don’t know how many characters the user enters?

Or am I doing

char mystring[400];

to enter a user up to 400 characters?

+5
source share
6 answers

You are faced with a specific problem with scanf () and% s - what happens when you don't know how much input there is?

char mystring[0];, . segfault. 0, , - , ( ) - segfault.

, 1: . (, ), char mystring[0], char *mystring.

, scanf, "% s" - - . :

char mystring[512];
scanf("%s", mystring);

511 ( 512th -\0), . :

scanf("%511s", mystring);

, , C , , . , .

- fgets().

:

while (fgets(mystring, 512, stdin))
{
   /* process input */
}

sscanf() mystring

5. , 4 , , . "" , fgets().

- , (, 10 ).

+6

, ( ). " " scanf, :

scanf("%50s", mystring);

51 50 . 50 , scanf 49 - .

+2

ggets(), C. . char, malloc(). stdin one char . , char realloc(), .

: http://cbfalconer.home.att.net/download/index.htm

.

+2

cbfalconer (http://cbfalconer.home.att.net/download/index.htm) :

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ggets.h"

#define INITSIZE   112  /* power of 2 minus 16, helps malloc */
#define DELTASIZE (INITSIZE + 16)

enum {OK = 0, NOMEM};

int fggets(char* *ln, FILE *f)
{
   int     cursize, ch, ix;
   char   *buffer, *temp;

   *ln = NULL; /* default */
   if (NULL == (buffer = malloc(INITSIZE))) return NOMEM;
   cursize = INITSIZE;

   ix = 0;
   while ((EOF != (ch = getc(f))) && ('\n' != ch)) {
      if (ix >= (cursize - 1)) { /* extend buffer */
         cursize += DELTASIZE;
         if (NULL == (temp = realloc(buffer, (size_t)cursize))) {
            /* ran out of memory, return partial line */
            buffer[ix] = '\0';
            *ln = buffer;
            return NOMEM;
         }
         buffer = temp;
      }
      buffer[ix++] = ch;
   }
   if ((EOF == ch) && (0 == ix)) {
      free(buffer);
      return EOF;
   }

   buffer[ix] = '\0';
   if (NULL == (temp = realloc(buffer, (size_t)ix + 1))) {
      *ln = buffer;  /* without reducing it */
   }
   else *ln = temp;
   return OK;
} /* fggets */
/* End of ggets.c */

int main(int argc, char **argv)
{
   FILE *infile;
   char *line;
   int   cnt;

   //if (argc == 2)
      //if ((infile = fopen(argv[1], "r"))) {
         cnt = 0;
         while (0 == fggets(&line, stdin)) {
            fprintf(stderr, "%4d %4d\n", ++cnt, (int)strlen(line));
            (void)puts(line);
            free(line);
         }
         return 0;
      //}
   //(void)puts("Usage: tggets filetodisplay");
   //return EXIT_FAILURE;
} /* main */
/* END file tggets.c */

, , .

+1

C , - GNU readline , , NetBSD, aka libedit. ( API, .)

scanf, fgets() , sscanf(). , .

0

, , , "mystring" 35 , . , . - , . , , , . , "mystring", malloc . , , ... .

0
source

All Articles