c - 如何用scanf跳过括号

标签 c

感谢您的帮助...但屏幕上仍然没有打印出 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/

相关文章:

c - 如何使 Gtk 程序可移植

包含不同值类型的 void 指针的 C 数组

objective-c - ObjC 访问 C 函数中的属性

c - 错误 : _mm_clmulepi64_si128 was not declared in this scope

如果缓冲区在 C 中有足够的空间,则将整数复制到缓冲区

c - 尝试从 GArray 检索 GHashTable 时未定义它

c - 检测与 scanf 格式字符串中常量的不匹配

android - 运行针对 Android 设备使用共享库的交叉编译 C 应用程序

c - 如何在c中打印环形输出?

c - 如何将套接字 recv(,,,) 用于不确定的响应长度