我正在尝试读取一个包含我的代码坐标值的文件。
我的问题是,如何使数组大小足够大以包含将来添加到文件中的内容?
以下是我的代码;当我将数组的大小设置为 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 );
}
}
}