Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.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/3/templates/2.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中使用scanf读取空白?_C_Whitespace_Scanf_Fgets - Fatal编程技术网

如何在c中使用scanf读取空白?

如何在c中使用scanf读取空白?,c,whitespace,scanf,fgets,C,Whitespace,Scanf,Fgets,问题:我需要能够识别两个空格何时连续出现 我已阅读以下问题: 我也知道scanf的问题: 输入将采用以下格式: 1 5 3 2 4 6 2 1 9 0 两个空格表示需要处理下一组数据,并与数据本身进行比较。行的长度未知,每组中的整数数未知。两个空格最多可分隔下一个数据集 虽然我可以使用FGET和各种内置函数来解决这个问题,但此时使用scanf解决问题可能会更容易。然而,如果不是这样,使用fgets、strtok和atoi将完成大部分工作,但我仍然需要识别一行中的两个空白 在输入一个非

问题:我需要能够识别两个空格何时连续出现

我已阅读以下问题:

我也知道scanf的问题:

输入将采用以下格式:

1 5 3 2  4 6 2  1 9  0
两个空格表示需要处理下一组数据,并与数据本身进行比较。行的长度未知,每组中的整数数未知。两个空格最多可分隔下一个数据集

虽然我可以使用FGET和各种内置函数来解决这个问题,但此时使用scanf解决问题可能会更容易。然而,如果不是这样,使用fgets、strtok和atoi将完成大部分工作,但我仍然需要识别一行中的两个空白

在输入一个非整数之前,下面的函数将取整数

while ( scanf ( "%d", &x ) == 1 ) 而(scanf(“%d”,&x)==1) 我需要它做的是读取空白,如果有两个连续的空白,我将让程序对下一组数据执行不同的操作

一旦我得到一个空白,我不知道该怎么说:

if ((input == "whitespace") && (previousInput == "whitespace")) ya da ya da else (input == "whitespace") ya da ya da else ya da ya da 如果((输入==“空白”)&&(以前的输入==“空白”)) 雅达雅达 else(输入=“空白”) 雅达雅达 其他的 雅达雅达 我感谢你抽出时间,感谢你的帮助

经验教训: 虽然下面是Jonathan Leffler发布的scanf解决方案,但使用getc的解决方案要简单一些(因为需要对内部scanf、正则表达式和字符的了解较少)。回想起来,如果对正则表达式有更好的了解,scanf和char当然会使问题更容易解决,当然,从一开始就知道哪些函数可用,哪些函数是最好的

while ( scanf ( "%c", &x ) == 1 )
使用
%c
可以读取空白字符,但只能读取所有数据并存储在数组中。然后分配
char*cptr
并将
cptr
设置为数组的开头,然后分析数组,如果您想读取十进制数,您只需在
cptr
上使用
sscanf
即可读取十进制数,但必须使指针在数组上的正确位置(在您想读取的数字上)


getc
ungetc
是你的朋友

#include <stdio.h>

int main(void) {
  int ch, spaces, x;
  while (1) {
    spaces = 0;
    while (((ch = getc(stdin)) != EOF) && (ch == ' ')) spaces++;
    if (ch == EOF) break;
    ungetc(ch, stdin);
    if (scanf("%d", &x) != 1) break;
    printf("%d was preceded by %d spaces\n", x, spaces);
  }
  return 0;
}
#包括
内部主(空){
int ch,空间,x;
而(1){
空格=0;
而((ch=getc(stdin))!=EOF)和(&(ch='')空格++;
如果(ch==EOF)中断;
ungetc(ch、stdin);
如果(扫描频率(“%d”,&x)!=1)中断;
printf(“%d前面有%d个空格\n”,x,空格);
}
返回0;
}
演示


编辑rahhhhhh。。。我把它上传为C++。这里是完全相同的事情,但是现在你对“空白”的定义是什么

坦白地说,我不想尝试使用
scanf()
来识别双空格;几乎所有其他方法都要简单得多

但是,如果您坚持执行不十分明智的操作,那么您可能希望使用从以下代码派生的代码:

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

int main(void)
{
    int d;
    char sp[3] = "";
    int n;

    while ((n = scanf("%d%2[ \t]", &d, sp)) > 0)
    {
        printf("n = %d; d = %d; sp = <<%s>>", n, d, sp);
        if (n == 2 && strlen(sp) == 2)
            printf(" end of group");
        putchar('\n');
    }
    return 0;
}
#包括
#包括
内部主(空)
{
int d;
char sp[3]=“”;
int n;
而((n=scanf(“%d%2[\t]”,&d,sp))>0)
{
printf(“n=%d;d=%d;sp=,n,d,sp”);
如果(n==2&&strlen(sp)==2)
printf(“集团结束”);
putchar('\n');
}
返回0;
}
方括号中包含字符类和2个字符,然后方括号坚持该类最多包含2个字符。您可能需要担心读取换行符并尝试获取更多数据以满足character类的要求,这可以通过从character类中删除换行符来解决。但这取决于您对空白的定义,以及组是否自动以换行符结尾。重置
sp[0]='\0'不会有什么坏处

或许,您最好反转字段,在数字之前检测两个空格。但在普通情况下,这将失败,因此,您可以使用简单的
%d”
格式读取数字(如果失败,您知道既没有空格也没有数字错误)。请注意,
%d
会占用前导空格(根据标准的定义)-所有空格


我看得越多,就越不喜欢“
scanf()
only”。请提醒我不要在贵校上课。

如果你真的想要
scanf
类型功能,你可以使用
fgets
sscanf
,并使用
%n
说明符获取scanf,以便在程序完成其余工作的同时,为程序提供每个空白范围的开头和结尾的偏移量


否则,请丢弃整个
scanf
族。在我看来,它很可能是标准库中最无用的部分。

这里有一个只使用scanf()函数的解决方案。在这个示例中,我使用sscanf()实现了大致相同的功能

#include <stdio.h>


int p_1_cnt = 0, p_2_cnt = 0;

void process_1(int x)
{
    p_1_cnt++;
}


void process_2(int x)
{
    p_2_cnt++;
}


char * input_line = "1 5 3 2  4 6 2  1 9  0";

int main(void)
{
    char * ip = input_line;

    int x = 0, ws_0 = 0, ws_1 = 0, preceding_spaces = 1, fields = -2;

    while (sscanf (ip, "%d%n %n", &x, &ws_0, &ws_1) > 0)
    {
        ip += ws_0;

        if ((preceding_spaces) == 1)
            process_1(x);
        else
            process_2(x);

        preceding_spaces = ws_1 - ws_0;
    }

    printf("\np_1_cnt = %d, p_2_cnt = %d", p_1_cnt, p_2_cnt);
    _fgetchar();

    return 0;
}
#包括
int p_1_cnt=0,p_2_cnt=0;
无效进程_1(int x)
{
p_1_cnt++;
}
无效过程_2(int x)
{
p_2_cnt++;
}
char*input_line=“1 5 3 2 4 6 2 1 9 0”;
内部主(空)
{
char*ip=输入线;
int x=0,ws_0=0,ws_1=0,前面的空间=1,字段=-2;
而(sscanf(ip、%d%n%n“、&x、&ws_0和&ws_1)>0)
{
ip+=ws_0;
if((前面的_空格)==1)
过程1(x);
其他的
过程2(x);
前面的_空间=ws_1-ws_0;
}
printf(“\np\u 1\u cnt=%d,p\u 2\u cnt=%d”,p\u 1\u cnt,p\u 2\u cnt);
_fgetchar();
返回0;
}

这是一种非常可怕的输入格式。如果你负责,重新设计它。如果,正如我所怀疑的,你被分配了一个家庭作业,那真是倒霉——他们是一群虐待狂,你的老师。注意“空白”和“两个空白”是不同的“空白”通常表示v
#include <stdio.h>


int p_1_cnt = 0, p_2_cnt = 0;

void process_1(int x)
{
    p_1_cnt++;
}


void process_2(int x)
{
    p_2_cnt++;
}


char * input_line = "1 5 3 2  4 6 2  1 9  0";

int main(void)
{
    char * ip = input_line;

    int x = 0, ws_0 = 0, ws_1 = 0, preceding_spaces = 1, fields = -2;

    while (sscanf (ip, "%d%n %n", &x, &ws_0, &ws_1) > 0)
    {
        ip += ws_0;

        if ((preceding_spaces) == 1)
            process_1(x);
        else
            process_2(x);

        preceding_spaces = ws_1 - ws_0;
    }

    printf("\np_1_cnt = %d, p_2_cnt = %d", p_1_cnt, p_2_cnt);
    _fgetchar();

    return 0;
}