gpt4 book ai didi

c - 如何强制 scanf 匹配空格?

转载 作者:太空宇宙 更新时间:2023-11-03 23:47:44 33 4
gpt4 key购买 nike

我正在尝试使用 fscanf() 读取一个必须前后有空格的字符:

fscanf( input, "%*[ \t]%c%*[ \t]", output )

但不幸的是,"%*[\t]" 格式说明符接受零个或多个匹配项。无论如何我可以要求它接受至少一个匹配项,还是我需要使用类似 getc() 的东西?

最佳答案

可以使用 fscanf() 解决这篇文章,但让我们看一下 fgetc() 方法。

// return 1 on success, else return 0
int GetSpaceCharSpace(FILE *istream, int *ch) {
*ch = fgetc(istream);
if (!isspace(*ch))
return 0;

// consume additional leading spaces as OP said "accept at least one match"
while (isspace(*ch = fgetc(istream)))
;
// Code has a non-white-space

// Success if next char is a white-space
return isspace(fgetc(istream));
}

关于c - 如何强制 scanf 匹配空格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28820758/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com