我正在尝试读取包含我的代码坐标值的文件。每次我使用 scanf 它只读取第一行...(60,70,200,200)。我的问题是如何让我的代码读取我文件的所有内容并将其打印在屏幕上。这是我的代码和文件。
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)
MY CODE:
#include <stdio.h>
int a;
int b;
int c;
int d;
int data[4][5];
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 i,j;
for (j=0; j < 5; j++)
{
for (i=0; i < 4; i++) {
fscanf(file, "S (%d,%d)(%d,%d)", &a, &b, &c, &d);
data[i][j] = (a, b, c, d);
printf("%d,%d,%d,%d\n",a, b, c, d);
}
}
fclose( file );
}
}
}