需要阅读从标准输入整数数量不明到一个数组 [英] Need to read an undetermined amount of integers from stdin into an array
问题描述
所以,我正在写一个程序的做法,将采取整数作为从标准输入,它们加载到一个数组,排序输出数组,然后将结果。
我一直在努力试图弄清楚IO用C如何工作的。这是我到目前为止,请让我知道如果您发现任何问题/有什么建议。理想情况下,我想避免使用缓冲区,但我似乎无法找到另一种方式,让大小不等的输入
输入格式:10 20 30 11 666 235 1...等
//检查是否指定了输入文件
//如果指定了一个,那么这些数字进行排序,从标准输入,否则拉
如果(INPUTFILE ==标准输入){ //加载标准输入到缓冲区
炭缓冲器[100];
如果(与fgets(缓冲区,缓冲区尺寸,标准输入)== NULL){
Handle_EOForIOError();
}
//获取数字的计数来创建数组
INT numIntegers = 0;
INT NUM;
而(sscanf的(安培;缓冲器[0],%d个,试验#)== 1){
numIntegers ++;
}
//初始化用适当大小的阵列
数=(INT *)malloc的(numIntegers *的sizeof(INT)); //将整数放入数组
INT I = 0;
而(sscanf的(安培;缓冲器[0],%d个,&放大器;数字由[i])== 1){
我++;
} }
的问题是,的sscanf
不保留一个指针到缓冲区。所以每次code调用的sscanf
它刚开从缓冲区中第一个数字。其结果是一个无限循环。
要解决这个问题,你可以使用%N
转换说明。 %N
返回已使用的转换,这种转换的字符数。这个数字(我称之为三角洲
在下面的code)可以用来更新索引缓冲区。下面是它如何工作的,每次调用的sscanf
返回三角洲
。这三角洲
添加到首页
和首页
在EX pression &放大器使用;缓冲[指数]
来指向缓冲区中的下一个数
下面是生成的code样子
//加载标准输入到缓冲区
炭缓冲器[100];
如果(与fgets(缓冲区,缓冲区尺寸,标准输入)== NULL)
Handle_EOForIOError();//获取数字的计数来创建数组
INT numIntegers = 0;
INT索引= 0;
INT NUM,三角洲;
而(sscanf的(安培;缓冲区[指数],%D%N,试验#,&安培;三角洲)== 1)
{
numIntegers ++;
指数+ =增量;
}//初始化用适当大小的阵列
为int *号=的malloc(numIntegers *的sizeof(INT));//将整数放入数组
索引= 0;
的for(int i = 0; I< numIntegers;我++)
{
如果(sscanf的(安培;缓冲区[指数],%D%N,&安培;数字[I],和放大器;!增量)= 1)
ComplainBitterlyAndExit();
指数+ =增量;
}
So I'm writing a practice program that will take integers as input from stdin, load them into an array, sort the array, and then output the results.
I've been struggling trying to figure out how IO in C works. Here's what I have so far, please let me know if you see any issues/have any suggestions. Ideally I'd like to avoid the use of a buffer, but I can't seem to find another way to allow variably sized input
Input format: "10 20 30 11 666 1 235" ... etc
// check if an input file is specified
// If one is specified, then sort those numbers, otherwise pull from stdin
if(inputFile == "stdin"){
// Load stdin into a buffer
char buffer[100];
if(fgets(buffer, sizeof(buffer), stdin) == NULL) {
Handle_EOForIOError();
}
// Get a count of the numbers to create the array
int numIntegers = 0;
int num;
while(sscanf(&buffer[0], "%d ", &num) == 1){
numIntegers++;
}
// Initialize the array with the proper size
numbers = (int*)malloc(numIntegers*sizeof(int));
// Load the integers into the array
int i = 0;
while(sscanf(&buffer[0], "%d ", &numbers[i]) == 1){
i++;
}
}
The problem is that sscanf
doesn't keep a pointer into the buffer. So every time the code calls sscanf
it's just getting the first number from the buffer. The result is an infinite loop.
To fix the problem, you can use the %n
conversion specifier. %n
returns the number of characters that were used by the conversions. That number (which I call delta
in the code below) can be used to update an index into the buffer. Here's how it works, each call to sscanf
returns a delta
. That delta
is added to an index
, and the index
is used in the expression &buffer[index]
to point to the next number in the buffer.
Here's what the resulting code look like
// Load stdin into a buffer
char buffer[100];
if(fgets(buffer, sizeof(buffer), stdin) == NULL)
Handle_EOForIOError();
// Get a count of the numbers to create the array
int numIntegers = 0;
int index = 0;
int num, delta;
while(sscanf(&buffer[index], "%d%n", &num, &delta) == 1)
{
numIntegers++;
index += delta;
}
// Initialize the array with the proper size
int *numbers = malloc(numIntegers*sizeof(int));
// Load the integers into the array
index = 0;
for ( int i = 0; i < numIntegers; i++ )
{
if (sscanf(&buffer[index], "%d%n", &numbers[i], &delta) != 1)
ComplainBitterlyAndExit();
index += delta;
}
这篇关于需要阅读从标准输入整数数量不明到一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!