从文件中检索数组并找到它的大小C.

时间:2021-08-21 21:43:10

I have a file that I have to read some numbers from and put them into an array. The only problem is that I don't know how to find the size of it. I am given the maximum size of the array but the numbers don't fill the array completely. I tried many different ways to make it work but it doesn't read the correct values from the file. Is there any other way to do it without sizeof?

我有一个文件,我必须从中读取一些数字并将它们放入一个数组中。唯一的问题是我不知道如何找到它的大小。我被赋予了数组的最大大小,但数字并没有完全填满数组。我尝试了许多不同的方法使其工作,但它没有从文件中读取正确的值。有没有其他方法可以做到没有sizeof?

#include<stdio.h>

#define MAX_NUMBER 25
int main(void)
{
int test[];
int size;

FILE* sp_input;    
int i;
sp_input = fopen("a20.dat", "r");

if (sp_input == NULL)
  printf("\nUnable to open the file a20.dat\n");
else
  {
  while( fscanf(sp_input, "%d", &test[i])!=EOF)
  {
  size=sizeof(test)/sizeof(test[0]);
  }

    for(i = 0; i < size; i++)

  printf("\na[%d]=%d has a size of %d\n", i,test[i],size);
  fclose(sp_input);    
  }

  return 0; 
}

2 个解决方案

#1


2  

If you increment i each time you successfully do a fscanf, it will serve as a count of the number of items read.

如果每次成功执行fscanf时增加i,它将作为读取项目数的计数。

i = 0;

while (fscanf(sp_input, "%d", &test[i]) == 1) {
    i = i + 1;
}

// Now, i is the number of items in the list, and test[0] .. test[i-1]
// are the items.

Edit: As @chux pointed out, in this case it's better to compare to 1, the expected number of items scanned, on each call. If a bogus input is provided (non-digits), there's still a problem and you should stop.

编辑:正如@chux指出的那样,在这种情况下,最好在每次调用时与1(预期扫描的项目数)进行比较。如果提供虚假输入(非数字),仍然存在问题,您应该停止。

#2


1  

Define a maximum size array and continue looping as able.

定义最大大小数组并继续循环。

File input need not fill the array, just populate it as it can. Keep track, i, of how many of test[] was used and be sure not to overfill the array.

文件输入不需要填充数组,只需填充它即可。跟踪i,使用了多少个test [],并确保不要过度填充阵列。

#define MAX_NUMBER 25
int test[MAX_NUMBER];

FILE* sp_input = fopen("a20.dat", "r");
...

// Use `size_t` for array indexing    
size_t i;
// do not read too many `int`    
for (i=0; i<MAX_NUMBER; i++) {
  if (fscanf(sp_input, "%d", &test[i]) != 1) {
    break;
  }
  printf("test[%zu]=%d\n", i, test[i]);
}

#1


2  

If you increment i each time you successfully do a fscanf, it will serve as a count of the number of items read.

如果每次成功执行fscanf时增加i,它将作为读取项目数的计数。

i = 0;

while (fscanf(sp_input, "%d", &test[i]) == 1) {
    i = i + 1;
}

// Now, i is the number of items in the list, and test[0] .. test[i-1]
// are the items.

Edit: As @chux pointed out, in this case it's better to compare to 1, the expected number of items scanned, on each call. If a bogus input is provided (non-digits), there's still a problem and you should stop.

编辑:正如@chux指出的那样,在这种情况下,最好在每次调用时与1(预期扫描的项目数)进行比较。如果提供虚假输入(非数字),仍然存在问题,您应该停止。

#2


1  

Define a maximum size array and continue looping as able.

定义最大大小数组并继续循环。

File input need not fill the array, just populate it as it can. Keep track, i, of how many of test[] was used and be sure not to overfill the array.

文件输入不需要填充数组,只需填充它即可。跟踪i,使用了多少个test [],并确保不要过度填充阵列。

#define MAX_NUMBER 25
int test[MAX_NUMBER];

FILE* sp_input = fopen("a20.dat", "r");
...

// Use `size_t` for array indexing    
size_t i;
// do not read too many `int`    
for (i=0; i<MAX_NUMBER; i++) {
  if (fscanf(sp_input, "%d", &test[i]) != 1) {
    break;
  }
  printf("test[%zu]=%d\n", i, test[i]);
}