是否可以使用scanf()读取文本行 - 排除\n和中断特殊(选定)字符,但包含该字符
这是我目前的表达方式:while(scanf("%49[^:\n]%*c", x)==1)
但这个排除了:.是否有可能打破阅读:但也读这个角色?
好的,我正在使用Johannes-Schaub-litb 的代码。
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;
}
Run Code Online (Sandbox Code Playgroud)
我仍然使用这个代码......并且它工作正常。稍后会稍微修改一下代码。
| 归档时间: |
|
| 查看次数: |
861 次 |
| 最近记录: |