繁体   English   中英

使用[^ ...]格式化fscanf格式化输入的格式?

[英]Correct format for fscanf formatted input using [^…]?

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

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

fp被打开并声明,并且此语句始终显示为false(扫描失败)。

思考?

我想你可能需要:

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

我写了一个小例子 随意更改它以满足您的需求:)

#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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM