gpt4 book ai didi

使用 [^...] 的 fscanf 格式化输入的格式是否正确?

转载 作者:行者123 更新时间:2023-11-30 14:28:46 25 4
gpt4 key购买 nike

我正在尝试读取格式为#string 1##string 2##....等的文件,并使用“#”符号作为唯一的分隔符。我还尝试将每个字符串复制到字符数组中。这是我当前的一些代码,但它似乎不起作用:

char temp[20];
if(fscanf(fp, "%15[^#]", temp ==1) ....

fp 被打开并声明,并且该语句总是显示为 false(扫描不成功)。

想法?

最佳答案

我写了一个little working example 。请随意更改它以满足您的需要:)

#include <stdio.h>
#include <string.h>

int main(void) {
char input[] = "#string 1##string two##three##last but one##five#";
char tmp[100];
char *pinput = input;
/* the conversion specification is
** %99[^#]
** the other '#' are literals that must be matched */
while (sscanf(pinput, "#%99[^#]#", tmp) == 1) {
printf("got [%s]\n", tmp);
pinput += strlen(tmp) + 2;
}
return 0;
}

关于使用 [^...] 的 fscanf 格式化输入的格式是否正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5592825/

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