Is it possible to read lines of text with scanf() - excluding \n
and break on special(chosen) character, but include that character
This is my current expression: while(scanf("%49[^:\n]%*c", x)==1)
but this one excludes :
.
Is it possible to break reading on :
but read that character too?
Ok I am using Johannes-Schaub-litb's 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;
}
Still I use this code ...and it works fine. The code will be modified a bit more later.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With