c - 从标准输出一次扫描两个数字

标签 c scanf scanning

我有一个程序可以将大量整数输出到标准输出,每个整数排成一行。例如:

103
104
105
107

我需要编写另一个程序来读取该数组并填充数字不是前一个数字增量 1 的空间。数字之间唯一的区别是 2 (105,107),这使它更容易。

这是我执行该逻辑的代码:

printf("d",num1);

if ((num2-num1) != 1)
    numbetween =  num1 + 1;
    printf("%d", numbetween);
    printf("%d", num2);
else(
    printf("%d",num2);
)

所以这个程序的输出现在是:

103
104
105
106
107

我的问题是阅读数字。我知道我可以执行 while (scanf("%hd", &num) != EOF) 一次读取所有行。但是要执行我想要的逻辑,我将需要一次读取两行并对它们进行计算,但我不知道该怎么做。

最佳答案

您始终可以只从文件中读取第一个和最后一个数字,然后打印其间的所有内容。

int main( void )
{
    // get the first value in the file
    int start;
    if ( scanf( "%d", &start ) != 1 )
        exit( 1 );

    // get the last value in the file
    int end = start;
    while ( scanf( "%d", &end ) == 1 )
        ;

    // print the list of numbers
    for ( int i = start; i <= end; i++ )
        printf( "%d\n", i );
}

关于c - 从标准输出一次扫描两个数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29812778/

相关文章:

java - 从 Java 读取 C 套接字挂起/阻塞

c - fscanf 的奇怪输入

c - 第二个 scanf 不会运行,程序刚刚终止。 C

asp.net - ASP.Net 的 Web TWAIN 扫描解决方案?

android - 使用 MediaScannerConnection scanFile 扫描下载的图像文件

qsort() 比较二维数组

在内核中异步调用 DeviceIoControl

c - 指向尚未声明的结构的指针

C 中的大小写敏感性 (scanf) (if/else)

c# - 威亚。在 Windows XP 中扫描彩色图像 c#