4

重複の可能性:
スペース文字を含む入力から文字列を読み取りますか?

文字列(技術的には文字配列)を入力として受け取る際に問題に直面しています。次の宣言があるとします。

 char* s;

「Enter」を押すまで、このcharポインターを使用して文字列を入力する必要があります。助けてください! 事前にサンクス。

4

6 に答える 6

9

C と C++ の両方fgetsで、新しい行まで文字列を読み取る関数を使用できます。例えば

char *s=malloc(sizeof(char)*MAX_LEN);
fgets(s, MAX_LEN, stdin);

あなたが望むことをします(Cで)。C++ では、コードは似ています

char *s=new char[MAX_LEN];
fgets(s, MAX_LEN, stdin);

C++std::stringは、文字の動的シーケンスであるクラスもサポートしています。文字列ライブラリの詳細: http://www.cplusplus.com/reference/string/string/ . 文字列を使用する場合は、次のように記述して行全体を読み取ることができます。

std::string s;
std::getline(std::cin, s);

場所: プロシージャは、ヘッダーまたは C++ で見つけることfgetsができ<string.h>ます<cstring>。この関数は、C 用、およびC++ 用mallocで見つけることができます。最後に、関数を含むクラスがファイルで見つかります。<stdlib.h><cstdlib>std::stringstd::getline<string>

アドバイス (C++ の場合): C スタイルの文字列と のどちらを使用するかわからない場合はstd::string、私の経験から、文字列クラスの方がはるかに使いやすく、より多くのユーティリティを提供し、はるかに高速であることをお伝えします。 C スタイルの文字列よりも。これはC++ 入門書の一部です:

As is happens, on average, the string class implementation executes considerably
faster than the C-style string functions. The relative average execution times on
our more than five-year-old PC are as follows:

    user            0.4   # string class
    user            2.55  # C-style strings
于 2012-12-24T15:06:05.410 に答える
2

最初に、メモリを割り当てる必要があるこの文字列に入力を取得します。その後、gets または fgets または scanf を使用できます

于 2012-12-24T14:49:57.793 に答える
1

C++ について考える場合は、cin.getline()役に立つかもしれません。

于 2012-12-24T14:53:13.650 に答える
1

cin>>variable_name; を使用できます。入力にスペースがない場合。スペースを含む入力の場合、c++ で gets(variable_name) を使用します

于 2012-12-24T15:21:07.297 に答える
0

塗りつぶしを開始するs前に割り当てる必要があります

1)入力文字列のサイズが事前にわからない場合は、使用できますrealloc

char* s = calloc(1,sizeof(char));
char t;
int len;
while(scanf("%c", &t)==1)
{
  if(t== '\n')
     break;
   len = strlen(s);
   s= realloc(s,len+1);
   *(s+len) = t;
   *(s+len+1) = '\0';
}

2)入力文字列の最大長が事前にわかっている場合は、次の方法で scanf を使用して文字列を char の配列に直接読み取ることができます。

char s[256] // Let's assume that the max length of your input string is 256

scanf("%[^\r\n]",s) // this will read your input characters till you heat enter even if your string contains spaces
于 2012-12-24T15:47:18.960 に答える
-1

うーん、OPが述べているので:

「Enter」を押すまで、このcharポインターを使用して文字列を入力する必要があります

これを試してみようと思ったのですが、頭の中で、これは動的バッファーであり、ポインターを使用mallocおよび使用しています。reallocただし、バグが含まれている可能性がありますが、要点はそこにあります。ニッピッキーはさておき...

コードがひどい場合は申し訳ありません... ::)

char *ptr = NULL, *temp_ptr = NULL;
int c = 0, chcount = 0, enter_pressed = 0;
do{
   c = fgetc(stdin);
   if (c != '\n' || c != -1){
     chcount++;
     if (ptr == NULL){
         ptr = malloc(sizeof(char) + chcount + 1);
         if (ptr != NULL) ptr[chcount] = c;
         else printf("ABORT!\n");
     }else{
         temp_ptr = realloc(ptr, chcount + 1);
         if (temp_ptr != NULL){
            ptr = temp_ptr;
            ptr[chcount] = c;
         }else{
            // OK! Out of memory issue, how is that handled?
            break;
         }
     }
   }else{
      enter_pressed = 1;
   }
}while (!enter_pressed);
ptr[chcount] = '\0'; // nul terminate it!
于 2012-12-24T15:32:35.260 に答える