【问题标题】:Correct format for fscanf formatted input using [^...]?使用 [^...] 的 fscanf 格式输入的正确格式?
【发布时间】:2011-04-08 09:00:07
【问题描述】:

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

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

fp 已打开并声明,此语句始终显示为 false(扫描不成功)。

想法?

【问题讨论】:

  • 它是否适用于良好的 ole scanf(或标准输入文件)?
  • 我刚刚注意到您正在传递以 1 作为参数的相等比较的结果。这是一个错字,还是你的实际代码?
  • 你需要绕过输入中的'#'

标签: c scanf


【解决方案1】:

我写了一个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;
}

【讨论】:

    【解决方案2】:

    我想你可能需要:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-30
      • 2017-04-28
      • 2021-08-19
      • 1970-01-01
      • 2022-01-23
      相关资源
      最近更新 更多