Hello! I used getline () to read a line from a stream, after reading it, the instruction continues to be executed and does not go to the new one. Is it possible to somehow indicate the character (pr: '\ n') on which he finishes reading. Tried with getdelim did not help.

char *line = NULL; int read = 0; while (read != -1) { puts("enter a line"); read = getline(&line, 0, stdin); printf("line = %s", line); printf("line length = %zu\n", read); puts(""); } free(line); 

    2 answers 2

    First you need to fix your use of getline . Man read? Who allowed you to pass 0 as the second argument?

    Also: why are you trying to print an int value using %zu format? (Although it is worth noting that the getline result should be taken in ssize_t , and not in int ).

    The second argument of getline requires a non-zero pointer to an object of type size_t containing the size of the allocated buffer (the input is ignored if the pointer is zero, but the output will save the amount of actually allocated memory). And printing values ​​of type int is done through the format %d

     char *line = NULL; size_t mem_size = 0; int read = 0; while (read != -1) { puts("enter a line"); read = getline(&line, &mem_size, stdin); printf("line = %s", line); printf("line length = %d\n", read); puts(""); } free(line); 

    Everything works as expected.

    However, printing the contents of the line after getline returned -1 , there is no point. If getline succeeds in reading something successfully, then it will not return -1 even if the reading has completed due to stumbling at the end of the file. Therefore, a more meaningful organization of such a cycle would be

     char *line = NULL; size_t mem_size = 0; do { puts("enter a line"); ssize_t read = getline(&line, &mem_size, stdin); if (read < 0) break; printf("line = %s", line); printf("line length = %zu\n", read); puts(""); } while (1); free(line); 

      The getline function getline declared as follows.

       ssize_t getline (char **lineptr, size_t *n, FILE *stream): 

      Therefore, your code has undefined behavior, since the second argument is a null pointer.

       read = getline(&line, 0, stdin); ^^^ 

      And in this sentence the format string is incorrect

       printf("line length = %zu\n", read); ^^^^ 

      since the read variable in your program is of type int , not size_t , as it should be.

      If the getline function getline value -1 , for example, when the end-of-file event occurs, however, the contents of the buffer in your program are still output to the console before this event is recognized in the while condition.

       char *line = NULL; int read = 0; while (read != -1) { puts("enter a line"); read = getline(&line, 0, stdin); // read уже равно -1, но буфер выводится на консоль printf("line = %s", line); printf("line length = %zu\n", read); //... 

      To interrupt line input, you can type Ctrl + z in Windows, or Ctrl + d in Unix. You can also enter an agreement that if the user enters a blank line by simply pressing the Enter key, this is also considered a sign of the end of the input.

      The program may, for example, look like this.

       #define _GNU_SOURCE #include <stdio.h> #include <stdlib.h> int main( void ) { char *line = NULL; size_t size = 0; while ( 1 ) { size_t n; printf( "Enter a line (empty string - exit): "); if ( ( n = getline( &line, &size, stdin ) ) < 0 || *line == '\n' ) break; printf( "line = %s", line ); printf( "line length = %zu\n", n ); puts(""); } free( line ); return 0; } 

      The dialogue with the program can be

       Enter a line (empty string - exit): Hello daniil.vorobjew2017 line = Hello daniil.vorobjew2017 line length = 27 Enter a line (empty string - exit): Have a nice day line = Have a nice day line length = 16 Enter a line (empty string - exit):