感谢您的帮助...但屏幕上仍然没有打印出 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 );
}
}
}
最佳答案
你把事情搞得太复杂了。只需按字面意思包含非数字部分即可:
while(fscanf(file, "S(%d,%d)", &a, &b) == 2)
printf("got S(%d,%d)\n", a, b);
另外,请注意 printf()
不支持模式,这没有多大意义。
关于c - 如何用scanf跳过括号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12511242/