-1

助けてくれてありがとう...しかし、それでも画面に300,400が出力されません。以下は私のフルコードです:コマンドプロンプトで渡されたファイル[ファイルの内容:S(300,400)]を読み取り、S、最初の括弧、コンマ、または最後の閉じ括弧を印刷または読み取りせずに、ファイルに含まれる2つの値を読み取ります

#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 );
        }
    }
}
4

3 に答える 3

4

あなたはそれをあまりにも複雑にしています。文字通り非数値部分を含めるだけです。

while(fscanf(file, "S(%d,%d)", &a, &b) == 2)
  printf("got S(%d,%d)\n", a, b);

printf()また、パターンをサポートしていないことに注意してください。これはあまり意味がありません。

于 2012-09-20T11:00:47.907 に答える
2

スキャンセットの要件はありません。次を使用するだけです。

while (2 == fscanf(file, "S(%d,%d)", &a, &b))
{
    printf("%d %d", a, b);
}

投稿されたコードは1つの整数(入力のみと戻り値)を読み取ることができますが、との両方を誤って使用する可能性が2あるため、の戻り値と比較してください。fscanf()fscanf()a1ab

于 2012-09-20T11:00:42.487 に答える
0

正規表現の解析とチェックにユーティリティを使用することをお勧めします。ここにオンラインがあります:http://www.regextester.com/

他にもたくさんあります...

于 2012-09-20T11:05:46.567 に答える