我想读取包含我的代码的坐标值的文件。如何动态增加数组大小
我的问题是,我如何使数组大小足够大,以包含将来添加到文件?
以下是我的代码;当我将数组设置为905的大小时,我的循环继续,直到空间填满。这是为什么?
FILE.txt
:
S (60,70)(200,200)
S (30,40)(100,200)
S (10,20)(80,10)
S (60,400)(700,200)
S (160,70)(240,20)
S (160,70)(240,20)
S (160,70)(240,20)
我的代码:
#include <stdio.h>
int a;
int b;
int c;
int d;
int data[905][4];
int main (int argc, char *argv[])
{
if (argc != 2) /* argc should be 2 for correct execution */
{
/* We print argv[0] assuming it is the program name */
printf("usage: %s filename", argv[0]);
}
else
{
// We assume argv[1] is a filename to open
FILE *file = fopen(argv[1], "r");
/* fopen returns 0, the NULL pointer, on failure */
if (file == 0)
{
printf("Could not open file\n");
}
else
{
int j=0;int count=1
for (j=0; j < count; j++)
{
fscanf(file, "S (%d,%d)(%d,%d)", &a, &b, &c, &d);
printf("%d,%d,%d,%d\n",a, b, c, d);
count++
}
fclose(file);
}
}
}
请尝试找到一个最简单的例子,它不清楚你的意思,想要什么,或者做错了什么......总的来说,你会调用'realloc()'或者做类似的事情.. –