标签: scanf

如何将字符串可移植转换为不常见的整数类型?

一些背景:如果我想用于,例如,scanf()将字符串转换为标准整数类型,比如uint16_t,我将使用SCNu16from <inttypes.h>,如下所示:

#include <stdio.h>
#include <inttypes.h>
uint16_t x;
char *xs = "17";
sscanf(xs, "%" SCNu16, &x);
Run Code Online (Sandbox Code Playgroud)

但是一个更不常见的整数类型就像pid_t没有任何这样的东西; 只支持普通的整数类型<inttypes.h>.要转换的另一种方式,可移植printf()一个pid_t,我可以把它转换为intmax_t和使用PRIdMAX,就像这样:

#include <stdio.h>
#include <inttypes.h>
#include <sys/types.h>
pid_t x = 17;
printf("%" PRIdMAX, (intmax_t)x);
Run Code Online (Sandbox Code Playgroud)

然而,似乎没有办法可移植scanf()到一个pid_t.所以这是我的问题:如何便携地这样做?

#include <stdio.h>
#include <sys/types.h>
pid_t x;
char *xs = 17;
sscanf(xs, "%u", &x);  /* Not portable! pid_t might not be int! /*
Run Code Online (Sandbox Code Playgroud)

我想到了scanf()一个 …

c string integer scanf c99

11
推荐指数
1
解决办法
1万
查看次数

如何在支持空字段的同时使用sscanf解析逗号分隔字符串中的字段?

我有一个逗号分隔的字符串,可能包含空字段.例如:

1,2,,4
Run Code Online (Sandbox Code Playgroud)

使用基本的

sscanf(string,"%[^,],%[^,],%[^,],%[^,],%[^,]", &val1, &val2, &val3, &val4);
Run Code Online (Sandbox Code Playgroud)

我得到空字段之前的所有值,以及从空字段开始的意外结果.

当我从sscanf()中删除空字段的表达式时,

sscanf(string,"%[^,],%[^,],,%[^,],%[^,]", &val1, &val2, &val3, &val4);
Run Code Online (Sandbox Code Playgroud)

一切都很好.

由于我不知道何时会得到一个空字段,有没有办法重写表达式以优雅地处理空字段?

c csv string parsing scanf

11
推荐指数
2
解决办法
5万
查看次数

在scanf之后fgets不起作用

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

void delspace(char *str);

int main() {
    int i, loops;
    char s1[101], s2[101];

    scanf("%d", &loops);

    while (loops--) {
        fgets(s1, 101, stdin);
        fgets(s2, 101, stdin);
        s1[strlen(s1)] = '\0';
        s2[strlen(s2)] = '\0';

        if (s1[0] == '\n' && s2[0] == '\n') {
            printf("YES\n");
            continue;
        }

        delspace(s1);
        delspace(s2);

        for (i = 0; s1[i] != '\0'; i++)
            s1[i] = tolower(s1[i]);

        for (i = 0; s2[i] != '\0'; i++)
            s2[i] = tolower(s2[i]);

        if (strcmp(s1, s2) == 0) {
            printf("YES\n");
        }
        else …
Run Code Online (Sandbox Code Playgroud)

c scanf fgets

11
推荐指数
3
解决办法
1万
查看次数

扫描long unsigned int的格式说明符

我试图使用此代码片段将两个变量作为输入: -

unsigned int i;

unsigned long int j;

scanf("%u",i);

scanf("%lu",j);
Run Code Online (Sandbox Code Playgroud)

但这引起了以下警告: -

警告:格式'%u'需要'unsigned int*'类型的参数,但参数2的类型为'unsigned int'[-Wformat]警告:格式'%lu'需要类型'long unsigned int*'的参数,但参数2有'long unsigned int'类型[-Wformat]任何人都可以向我解释这里发生了什么?

c scanf

11
推荐指数
1
解决办法
6万
查看次数

在C中读取单个字符

我正在尝试从控制台读取一个字符(在while循环中).但它不止一次地读过.

输入:

a
Run Code Online (Sandbox Code Playgroud)

输出:

char : a  char : char : '
Run Code Online (Sandbox Code Playgroud)

码:

while(..)
{
    char in;
    scanf("%c",&in);
}
Run Code Online (Sandbox Code Playgroud)

我怎么才能读'a'?

c scanf

11
推荐指数
2
解决办法
8万
查看次数

使用%f,%e,%g,%E或%G与scanf有什么区别?

在C中,当用于使用scanf读入浮点变量时,格式说明符%f,%e,%g,%E和%G是否有任何差异?也就是说,代码片段的行为是否会发生

float x;
scanf("%<one of f, e, g, E, G>", &x);
Run Code Online (Sandbox Code Playgroud)

是否依赖于说明者的选择?

我首先假设%f只能正确解释十进制表示法,而%e只能正确解释科学记数法,但在我的系统中,每种情况下都可以正常工作.但也许我的系统只是自由主义......

我在书本或网上找不到任何明确的陈述......

c floating-point scanf

11
推荐指数
1
解决办法
3万
查看次数

用于使用sscanf解析输入的C++替代方法

假设我的程序需要表单的参数[ 0.562 , 1.4e-2 ](即浮点数对),我应该如何在没有正则表达式的情况下用C++解析这个输入?我知道在用户输入时需要考虑很多极端情况,但我们假设给定输入与上述格式紧密匹配(除了更多的空格).

在C中,我可以做一些类似于sscanf(string, "[%g , %g]", &f1, &f2);提取两个浮点值的东西,这非常紧凑.

在C++中,这是我到目前为止所提出的:

std::string s = "[ 0.562 , 1.4e-2 ]"; // example input

float f1 = 0.0f, f2 = 0.0f;

size_t leftBound = s.find('[', 0) + 1;
size_t count = s.find(']', leftBound) - leftBound;

std::istringstream ss(s.substr(leftBound, count));
string garbage;

ss >> f1 >> garbage >> f2;

if(!ss)
  std::cout << "Error while parsing" << std::endl;
Run Code Online (Sandbox Code Playgroud)

我怎么能改进这段代码?特别是,我关注garbage字符串,但我不知道如何跳过,这两个值之间.

c++ parsing scanf

11
推荐指数
2
解决办法
9566
查看次数

scanf()可变长度说明符

如何使用变量指定scanf()应读入的最大字符数?

例如使用printf()你可以像这样使用*

#define MAXVAL 5
printf("Print at maximum MAXVAL chars: %.*s\n", MAXVAL, "myStringHere");
Run Code Online (Sandbox Code Playgroud)

这只会打印5个字符,如何才能scanf在MAXVAL中读取?MAXVAL必须用作长度说明符.我不能简单地做

scanf("%5s", string);
Run Code Online (Sandbox Code Playgroud)

现在我只能想到读入一个大数组scanf然后ssprintf用来将字符串存储到我的长度有限的字符串中.然而,使用长度说明符会更容易.

c scanf

11
推荐指数
2
解决办法
8381
查看次数

如何scanf只有整数?

我希望代码运行,直到用户输入整数值.

该代码适用于char和char数组.

我做了以下事情:


#include<stdio.h>
int main()
{
    int n;
    printf("Please enter an integer: ");
    while(scanf("%d",&n) != 1)
    {
        printf("Please enter an integer: ");
        while(getchar() != '\n');
    }
    printf("You entered: %d\n",n);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

问题是如果用户输入一个浮点值scanf就会接受它.

Please enter an integer: abcd
Please enter an integer: a
Please enter an integer: 5.9
You entered: 5
Run Code Online (Sandbox Code Playgroud)

怎么可以避免?

c scanf

11
推荐指数
4
解决办法
13万
查看次数

如果我忘记关闭扫描设置会发生什么?

假设我忘了关闭]扫描仪的右方括号.那会发生什么?它会调用未定义的行为吗?

例:

char str[] = "Hello! One Two Three";
char s1[50] = {0}, s2[50] = {0};
sscanf(str, "%s %[^h", s1, s2); /* UB? */
printf("s1='%s' s2='%s'\n", s1, s2);
Run Code Online (Sandbox Code Playgroud)

我在编译时收到GCC的警告:

source_file.c: In function ‘main’:
source_file.c:11:5: warning: no closing ‘]’ for ‘%[’ format [-Wformat=]
     sscanf(str, "%s %[^h", s1, s2); /* UB? */
Run Code Online (Sandbox Code Playgroud)

和输出为

s1='Hello!' s2=''
Run Code Online (Sandbox Code Playgroud)

我也注意到了sscanf回报1.但到底是怎么回事?

我检查了C11标准,但没有找到与此相关的信息.

c scanf format-specifiers

11
推荐指数
1
解决办法
132
查看次数

标签 统计

scanf ×10

c ×9

parsing ×2

string ×2

c++ ×1

c99 ×1

csv ×1

fgets ×1

floating-point ×1

format-specifiers ×1

integer ×1