Is there any function to get unlimited input string from standard input

Condition:

I want to enter a string from standard input, and I don’t know its size, maybe a very long time.

a method like scanf , gets should know the maximum length you can enter, so the size of your input is less than the size of your buffer.

So, are there any good ways to handle this?

The answer should only be in C, not C ++ , so the C ++ line is not what I want. I want this to be a standard C string, something like char* and ends with '\0' .

+5
source share
4 answers

The C standard does not define such a function, but POSIX does.

The getline function described here (or by typing man getline if you are using a UNIX-like system) does what you ask.

It may not be available on systems other than POSIX (for example, on MS Windows).

A small program demonstrating its use:

 #include <stdio.h> #include <stdlib.h> int main(void) { char *line = NULL; size_t n = 0; ssize_t result = getline(&line, &n, stdin); printf("result = %zd, n = %zu, line = \"%s\"\n", result, n, line); free(line); } 

As with fgets , the character '\n' newline remains in the array.

+5
source

One way is to start the loop with getchar and continue to put the characters in the array. When the array is full, realloc eat it at a larger size.

+3
source

scanf often ignores the conversion specification , which allocates enough memory to hold string input regardless of length. For later versions of scanf use m . Older versions a . For instance:

 #include <stdio.h> #include <stdlib.h> int main (void) { char *str = NULL; printf (" enter a string of any length, whitespace is OK: "); scanf ("%m[^\n]%*c", &str); printf ("\n str: %s\n\n", str); if (str) free (str); return 0; } 

Note. scanf requires a char ** pointer argument to get the highlighted string. Also note scanf does not include '\n' in the saved string. Note the %*c , which accepts and discards the character '\n' to prevent a new line from appearing in the input buffer. You may also be preceded by a conversion specifier with a space to skip any / all white space that may exist in the input buffer.

In conclusion Note: There are reports that not all scanf implementations offer this feature. (which can also be a confusion of the m/a change). Check your implementation.

+1
source

One of the methods is to use the getchar () function, which we can get in the symbol and pass it to the dynamically created array. You can see that when it exceeds the default length that we set, we redistributed the space for storing the character

 #include<stdio.h> #include<stdlib.h> void main(){ int size = 10; char* str; str = (char*) calloc(size,sizeof(char)); char c; c = getchar(); int t = 0; int cnt = 0; int len; while(c!='\n') { if(cnt > size) { str = (char*) realloc(str,2*cnt); } str[t] = c; c = getchar(); t++; cnt++; } str[t]='\0'; printf("The string is %s\n",str); len = strlen(str); printf("The size is %d",len); } 
0
source

Source: https://habr.com/ru/post/1208982/


All Articles