Use scanf to read lines or break a special character

Is it possible to read lines of text using scanf () - excluding \n and splitting into a special (selected) character, but including this character

This is my current expression: while(scanf("%49[^:\n]%*c", x)==1) but this excludes : Is it possible to break the reading into : but also read this symbol?

+6
source share
4 answers

Ok, I'm using Johannes-Schaub-litb code.

 char * getline(char cp) { char * line = malloc(100), * linep = line; size_t lenmax = 100, len = lenmax; int c; if(line == NULL) return NULL; for(;;) { c = fgetc(stdin); if(c == EOF) break; if(--len == 0) { len = lenmax; intptr_t diff = line - linep; char * linen = realloc(linep, lenmax *= 2); if(linen == NULL) { free(linep); return NULL; } line = linen + diff; linep = linen; } if((*line++ = c) == cp) break; } *line = '\0'; return linep; } 

However, I use this code ... and it works great. The code will be changed a bit later.

+4
source

I did it a little differently. Perhaps this can lead to crashes in the windows. But, anyway, here it is. Hope it helps. Merry Christmas.

 #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char *input_str; /** * Dynamic memory allocation. * Might crash on windows. */ int status = scanf("%m[^.]", &input_str); /** * If the first character is the * terminating character then scanf scans nothing * and returns 0. */ if (status > 0) { /** * Calculate the length of the string. */ size_t len = strlen(input_str); /** * While allocating memory provide * two extra cell. One for the character * you want to include. * One for the NULL character. */ char *new_str = (char*) calloc (len + 2, sizeof(char)); /** * Check for memory allocation. */ if(new_str == NULL) { printf("Memory Allocation failed\n"); exit(1); } /** * Copy the string. */ strcpy(new_str, input_str, len); /** * get your desired terminating character * from buffer */ new_str[len++] = getc(stdin); /** * Append the NULL character */ new_str[len++] = '\0'; /** * eat other characters * from buffer. */ while(getc(stdin) != '\n'); /** * Free the memory used in * dynamic memory allocation * in scanf. Which is a must * according to the scanf man page. */ free(input_str); } else { char new_str[2] = ".\0"; /** * eat other characters * from buffer. */ while(getc(stdin) != '\n'); } } 

EDIT: - I used the dot as a termination character.

0
source

Is it possible to read lines of text using scanf () - excluding \ n and splitting it into a special (selected) character, but including this character (?)

Yes. But scanf() is known for being used incorrectly and is difficult to use correctly. Of course, the scanf() approach will work for most users. Only a rare implementation is fully consistent with the OP goal without missing corner cases. IMO, this is not worth it.

As an alternative, we will try a direct approach, we will repeatedly use fgetc() . Invalid code:

 char *luvatar_readline(char *destination, size_t size, char special) { assert(size > 0); char *p = destitution; int ch; while (((ch = fgetc(stdin)) != EOF) && (ch != '\n')) { if (size > 1) { size--; *p++ = ch; } else { // Ignore extra input or // TBD what should be done if no room left } if (ch == (unsigned char) special) { break; } } *p = '\0'; if (ch == EOF) { // If rare input error if (ferror(stdin)) { return NULL; } // If nothing read and end-of-file if ((p == destination) && feof(stdin)) { return NULL; } } return destination; } 

Using example

 char buffer[50]; while (luvatar_readline(buffer, sizeof buffer_t, ':')) { puts(buffer); } 

Corner cases of TBD: It is not clear what the OP wants if special is '\n' or '\0' .


OP while(scanf("%49[^:\n]%*c", x)==1) has many problems.

  • Cannot cope with input, starts with : or '\n' , leaving x unset.

  • Doesn't know if the character after the input : non '\n' equal to : '\n' , EOF.

  • Does not consume additional input for 49.

  • Uses a fixed spatial character ':' , not a common one.

0
source

I think you want to do this:

 #include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { char *line = NULL; size_t len = 0; ssize_t read; while ((read = getline(&line, &len, stdin)) != -1) { if (read > 0 && line[read - 1] == '\n') { if (read > 1 && line[read - 2] == '\r') { line[read - 2] = '\0'; // we can remove the carriage return } else { line[read - 1] = '\0'; // we can remove the new line } } char const *delim = ":"; printf("parsing line :\n"); char *token = strtok(line, delim); while (token != NULL) { printf("token: %s\n", token); token = strtok(NULL, delim); } } free(line); } 
0
source

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


All Articles