4

scanf メソッドのみに制限されたクラス用のプログラムを作成します。プログラム受信は、入力として任意の数の行を受信できます。scanf で複数行の入力を受け取る際の不具合。

#include <stdio.h>
int main(){
    char s[100];
    while(scanf("%[^\n]",s)==1){
        printf("%s",s);
    }
    return 0;
}

入力例:

Here is a line.
Here is another line.

これは現在の出力です:

Here is a line.

出力を入力と同じにしたい。scanf を使用します。

4

4 に答える 4

9

あなたが望むのは次のようなものだと思います(実際にscanfのみに制限されている場合):

#include <stdio.h>
int main(){
    char s[100];
    while(scanf("%[^\n]%*c",s)==1){
        printf("%s\n",s);
    }
    return 0;
}

%*c は、基本的に入力の最後の文字を抑制します。

からman scanf

An optional '*' assignment-suppression character: 
scanf() reads input as directed by the conversion specification, 
but discards the input.  No corresponding pointer argument is 
required, and this specification is not included in the count of  
successful assignments returned by scanf().

[編集:クリス・ドッドのバッシングに従って誤解を招く回答を削除しました:)]

于 2013-01-24T05:16:45.083 に答える
6

このコードを試して、タブキーを区切り記号として使用してください

#include <stdio.h>
int main(){
    char s[100];
    scanf("%[^\t]",s);
    printf("%s",s);

    return 0;
}
于 2013-01-24T06:56:54.950 に答える
1

ヒントをあげます。

「EOF」条件に達するまで scanf 操作を繰り返す必要があります。

通常行われる方法は、

while (!feof(stdin)) {
}

構築します。

于 2013-01-24T05:12:27.933 に答える
0

このコードを試してみてください.C99標準のGCCコンパイラで期待どおりに動作します..

#include<stdio.h>
int main()
{
int s[100];
printf("Enter multiple line strings\n");
scanf("%[^\r]s",s);
printf("Enterd String is\n");
printf("%s\n",s);
return 0;
}
于 2017-03-06T11:41:22.157 に答える