感谢您的帮助......但它仍然没有在屏幕上打印出 300,400。下面是我的完整代码:读取在命令提示符处传递的文件[文件包含:S(300,400)] 并读取其中包含的两个值,而不打印或读取 S、第一个括号、逗号或最后一个右括号
#include <stdio.h>
int a;
int b;
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
{
/*reading file..
while (fscanf(file, "S(%d,%d)", &a, &b) == 2)
{
printf("%d,%d", a, b);
}
fclose( file );
}
}
}