Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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
使用fscanf接收阵列_C_Arrays_Scanf_Character Arrays - Fatal编程技术网

使用fscanf接收阵列

使用fscanf接收阵列,c,arrays,scanf,character-arrays,C,Arrays,Scanf,Character Arrays,我试图从一个文件中获取浮点数的输入,并将其排列成一个数组 唯一的问题是我不知道每次会有多少浮点数,尽管我知道浮点数的最大值是1000 我需要做的是让fscanf接受所有的浮点数,然后停在下一行,这是充满整数的 以下是我到目前为止对本节的了解: for (repetition = 0; repetition <= 1000; repetition++) { fscanf(userFile, "%f", &itemPrice[itemNumber]); itemNu

我试图从一个文件中获取浮点数的输入,并将其排列成一个数组

唯一的问题是我不知道每次会有多少浮点数,尽管我知道浮点数的最大值是1000

我需要做的是让fscanf接受所有的浮点数,然后停在下一行,这是充满整数的

以下是我到目前为止对本节的了解:

for (repetition = 0; repetition <= 1000; repetition++)
{ 

    fscanf(userFile, "%f", &itemPrice[itemNumber]);
    itemNumber++;

}

for(repeation=0;repeationfscanf
的返回值是成功读取的项目数。使用该值决定何时停止

#include <stdio.h>

int main(int argc,char * argv[])
{
  int i;
  float ff[1000];
  char next_text[17];

  for (i=0; i < 1000; i++) {
    int n_read;

    n_read = fscanf(stdin, " %f", &( ff[i] ));
    if (n_read < 1) {
      fscanf(stdin, "%16s", next_text);
      next_text[16] = (char) 0;
      printf("Next text: '%s'\n", next_text);
      break;
    }
  }

  printf("Read %d items, %f .. %f\n",i,ff[0],ff[i-1]);
  return 0;
}
#包括
int main(int argc,char*argv[])
{
int i;
浮动ff[1000];
char next_text[17];
对于(i=0;i<1000;i++){
int n_-read;
n_read=fscanf(标准输入,%f,&(ff[i]);
如果(n_读数<1){
fscanf(标准输入,“%16s”,下一个文本);
下一个文本[16]=(字符)0;
printf(“下一个文本:'%s'\n',下一个文本);
打破
}
}
printf(“读取%d项,%f..%f\n”,i,ff[0],ff[i-1]);
返回0;
}

也许您可以使用fgetc和fputc尝试此方法:

for (repetition = 0; itemPrice <= 1000; repetition++)
{
    int c = fgetc(userFile);
    if (c == '\n' || c == EOF) break;
    fputc(c);


    fscanf(userFile, "%f", &itemPrice[itemNumber]);
    itemNumber++;

}

for(repeation=0;itemPriceOP相当接近。扫描失败时停止

fscanf()
返回扫描的字段数或
EOF
。在这种情况下,检查
1
就足够了

// for (repetition = 0; itemPrice <= 1000; repetition++)  Use <, not <= @Arpit
for (i = 0; i < 1000; )
{ 
    int retval = fscanf(userFile, "%f", &itemPrice[i]);
    if (retval != 1) break;
    i++;
}
另一种方法是一次直接从文件1
float
中读取,然后查看下面的空白作为行尾

for (i = 0; i < 1000; ) { 
    if (fscanf(input, "%f", &itemPrice[i]) != 1) {
      // need to add code to consume the rest of the line if processing is to continue.
      break;
      }
    i++;

    // look for standard white-space
    char buf[2];
    while (fscanf(input, "%1[ \f\n\r\t\v]", buf) == 1) {
      if (buf[0] == '\n') break;
    }
}   
foo(itemPrice, i); // Use data
(i=0;i<1000;)的

如果(fscanf(输入、%f、&itemPrice[i])!=1){
//如果要继续处理,则需要添加代码以使用行的其余部分。
打破
}
i++;
//寻找标准的空白
char-buf[2];
而(fscanf(输入,“%1[\f\n\r\t\v]”,buf)=1){
如果(buf[0]='\n')中断;
}
}   
foo(itemPrice,i);//使用数据

循环条件下,您已经执行了
itemPrice。您知道所有浮点数都将在同一行上,并且该行上不会有其他任何内容吗?
itemPrice
itemPrice<1000
我正在处理代码,我将其更改为
itemPrice。我尝试了这一点,但它仍然在保存作为浮点数数组的一部分,下一行的e整数
itemPrice[1000]
@Dakrid建议发布一个简短的文件输入示例。顺便说一句:一定要使用
这是因为数组从0开始吗?因此0-999将==1000个值?@Dakrid是的,数组从0开始索引。
对于(repeation=0;itemPrice,因此我将其插入并调整它以适合我的程序声明,只是我得到了“对'foo'的不精确引用”
for (i = 0; i < 1000; ) { 
    if (fscanf(input, "%f", &itemPrice[i]) != 1) {
      // need to add code to consume the rest of the line if processing is to continue.
      break;
      }
    i++;

    // look for standard white-space
    char buf[2];
    while (fscanf(input, "%1[ \f\n\r\t\v]", buf) == 1) {
      if (buf[0] == '\n') break;
    }
}   
foo(itemPrice, i); // Use data
char input[MAX];
while(fgets(input, MAX, userFile) != NULL){
    sscanf(input, "%lf", &itemPrice[itemNumber++]);
}