Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 读格式化输入_C++_C_Debugging - Fatal编程技术网

C++ 读格式化输入

C++ 读格式化输入,c++,c,debugging,C++,C,Debugging,我正在尝试读取以下类型的输入: [Some text] (x,y) 我需要把x,y存储成整数 我尝试了以下代码: #include<iostream> #include <cstdio> using namespace std; int main(){ char temp[20]; int x1, x2; scanf("%[^(]%d,%d)", temp, &x1, &x2); printf("%s %d %d", t

我正在尝试读取以下类型的输入:

[Some text] (x,y)
我需要把x,y存储成整数

我尝试了以下代码:

#include<iostream>
#include <cstdio>
using namespace std;

int main(){
    char temp[20];
    int x1, x2;
    scanf("%[^(]%d,%d)", temp, &x1, &x2);
    printf("%s %d %d", temp,x1,x2);
    return 0;
}
错误是什么?

因为发生了
问题。您需要跳过该字符

使用:
scanf(“%[^(](%d,%d)”,temp,&x1,&x2);
,因为发生了
问题。您需要跳过该字符

使用:
scanf(“%d,%d)”,temp,&x1,&x2);
“%[^(]%d,%d)”
->
“%[^(](%d,%d)”
“%[^(]%d,%d)”
您忘记了一个paren
<

读取not-
s)后,在读取
int
s之前,您希望先消耗一个

编辑正如H2CO3善意地提到的:使用和或,它们非常

您忘记了一个参数

读取not-
s)后,在读取
int
s之前,您希望先消耗一个

编辑如H2CO3所述:使用和或,它们非常重要

这告诉
scanf()

  • 读取所有不是左(左)括号的字符

  • 然后读取一个十进制整数

  • 然后读一个逗号

  • 然后读取另一个十进制整数

  • 然后使用后面的右括号

  • 缺少的是,在阅读前导文本后,您实际上并没有阅读开头部分。因此,请更改格式字符串以包含以下内容:

    %[^(](%d,%d)
    

    或者更好的是,考虑手动解析字符串。<代码> SCANFER()/Field>格式字符串为“强>晦涩\ < /强>,并且很容易犯一个小错误,然后整个事件都会发生(<强>刚刚发生在<>强>)。

    char buf[LINE_MAX];
    fgets(buf, sizeof(buf), stdin);
    const char *lparen = strchr(buf, '(');
    const char *comma = strchr(lparen + 1, ',');
    // const char *rparen = strchr(comma + 1, ')'); // is this even needed?
    
    char str[lparen - buf + 1];
    memcpy(str, buf, lparen - buf);
    str[lparen - buf] = 0;
    int n1 = strtol(lparen + 1, NULL, 10);
    int n2 = strtol(comma + 1, NULL, 10);
    

    这告诉
    scanf()

  • 读取所有不是左(左)括号的字符

  • 然后读取一个十进制整数

  • 然后读一个逗号

  • 然后读取另一个十进制整数

  • 然后使用后面的右括号

  • 缺少的是,在阅读前导文本后,您实际上并没有阅读开头部分。因此,请更改格式字符串以包含以下内容:

    %[^(](%d,%d)
    

    或者更好的是,考虑手动解析字符串。<代码> SCANFER()/Field>格式字符串为“强>晦涩\ < /强>,并且很容易犯一个小错误,然后整个事件都会发生(<强>刚刚发生在<>强>)。

    char buf[LINE_MAX];
    fgets(buf, sizeof(buf), stdin);
    const char *lparen = strchr(buf, '(');
    const char *comma = strchr(lparen + 1, ',');
    // const char *rparen = strchr(comma + 1, ')'); // is this even needed?
    
    char str[lparen - buf + 1];
    memcpy(str, buf, lparen - buf);
    str[lparen - buf] = 0;
    int n1 = strtol(lparen + 1, NULL, 10);
    int n2 = strtol(comma + 1, NULL, 10);
    

    这就是为什么应该使用
    fgets()
    strchr()
    而不是
    scanf()
    的原因。H2CO3也写了一个鼓励这一点的答案:这就是为什么
    fgets()
    strchr()
    应该使用而不是
    scanf()
    。H2CO3也写了一个鼓励这一点的答案:p
    char buf[LINE_MAX];
    fgets(buf, sizeof(buf), stdin);
    const char *lparen = strchr(buf, '(');
    const char *comma = strchr(lparen + 1, ',');
    // const char *rparen = strchr(comma + 1, ')'); // is this even needed?
    
    char str[lparen - buf + 1];
    memcpy(str, buf, lparen - buf);
    str[lparen - buf] = 0;
    int n1 = strtol(lparen + 1, NULL, 10);
    int n2 = strtol(comma + 1, NULL, 10);