Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何将一个字符串从缓冲区添加到另一个数组并打印它?_C++_C_Loops_Buffer_Fgets - Fatal编程技术网

C++ 如何将一个字符串从缓冲区添加到另一个数组并打印它?

C++ 如何将一个字符串从缓冲区添加到另一个数组并打印它?,c++,c,loops,buffer,fgets,C++,C,Loops,Buffer,Fgets,所以基本上我想写一个字,当我按下enter键时,我想把这个字符串从缓冲区存储到数组中。当我写第一个单词时,它可能会起作用,但当我想添加第二个单词时,它会变得棘手 e.g.1st input is:first\0 2nd input is:two\0 then:Ctrl+Z(to get out of the loop) Output I want:first two (actually printing the 'array' array.) 我的代码在这里: posi

所以基本上我想写一个字,当我按下enter键时,我想把这个字符串从缓冲区存储到数组中。当我写第一个单词时,它可能会起作用,但当我想添加第二个单词时,它会变得棘手

e.g.1st input is:first\0
    2nd input is:two\0
    then:Ctrl+Z(to get out of the loop)
    Output I want:first two (actually printing the 'array' array.)
我的代码在这里:

position=0;
printf("Enter a text: \n");
while(fgets(buffer, 100 , stdin) != NULL){
    for (i=position;i<(position+numberOfChars);i++){
        array[i]=buffer[i];
    }
    numberOfChars=strlen(buffer);
    position=position+numberOfChars+1;
}
位置=0;
printf(“输入文本:\n”);
while(fgets(buffer,100,stdin)!=NULL){

关于(i=位置;i见代码中的注释:

position=0;
printf("Enter a text: \n");
while(fgets(buffer, 100 , stdin) != NULL){

    /* because you want to add space between 'first' and 'two' */ 
    if (position != 0) {
        array[position] = ' ';
        position++;
    }

    /* you need to get the buffer len for THIS iteration */
    numberOfChars=strlen(buffer);

    for (i=position;i<(position+numberOfChars);i++){
        /* i is a valid indice for array but not for buffer[0..numberOfChars-1] */
        /* array[i]=buffer[i]; */
        array[i] = buffer[i-position];
    }

    /* adding one will not add a space char */
    /* position=position+numberOfChars+1; */
    position = position+numberOfChars;
}

/* finaly add the null char at the end of the string (string is null terminated) */
array[position] = '\0';

请参见代码中的注释:

position=0;
printf("Enter a text: \n");
while(fgets(buffer, 100 , stdin) != NULL){

    /* because you want to add space between 'first' and 'two' */ 
    if (position != 0) {
        array[position] = ' ';
        position++;
    }

    /* you need to get the buffer len for THIS iteration */
    numberOfChars=strlen(buffer);

    for (i=position;i<(position+numberOfChars);i++){
        /* i is a valid indice for array but not for buffer[0..numberOfChars-1] */
        /* array[i]=buffer[i]; */
        array[i] = buffer[i-position];
    }

    /* adding one will not add a space char */
    /* position=position+numberOfChars+1; */
    position = position+numberOfChars;
}

/* finaly add the null char at the end of the string (string is null terminated) */
array[position] = '\0';
请注意,fgets()还将从stdin读取的换行符存储到缓冲区中。因此,您希望将其删除。您希望在两个字之间留出额外的空间。此外,您初始化numberOfChars太晚了。它应该在复制循环中使用之前发生

试试这个:

position = 0;
printf("Enter a text: \n");
while(fgets(buffer, 100 , stdin) != NULL) {
    numberOfChars = strlen(buffer);
    buffer[numberOfChars - 1] = ' '; //this replaces newline with space
    // note that i <= in the condition; to copy the '\0' over
    for (i = position; i <= (position + numberOfChars); i++) {
        array[i] = buffer[i];
    }        
    position = position + numberOfChars;
}
位置=0;
printf(“输入文本:\n”);
while(fgets(buffer,100,stdin)!=NULL){
numberOfChars=strlen(缓冲区);
缓冲区[numberOfChars-1]='';//这将用空格替换换行符
//请注意,我请注意,fgets()还将从stdin读取的换行符存储到缓冲区中。因此,您希望将其删除。您希望在两个字之间留出额外的空间。此外,您初始化numberOfChars太晚了。它应该在复制循环中使用之前发生

试试这个:

position = 0;
printf("Enter a text: \n");
while(fgets(buffer, 100 , stdin) != NULL) {
    numberOfChars = strlen(buffer);
    buffer[numberOfChars - 1] = ' '; //this replaces newline with space
    // note that i <= in the condition; to copy the '\0' over
    for (i = position; i <= (position + numberOfChars); i++) {
        array[i] = buffer[i];
    }        
    position = position + numberOfChars;
}
位置=0;
printf(“输入文本:\n”);
while(fgets(buffer,100,stdin)!=NULL){
numberOfChars=strlen(缓冲区);
缓冲区[numberOfChars-1]='';//这将用空格替换换行符

//注意,C或C++,因为C++容器类比使用MalC/ReAlLc和指针数组/ RealOrror更容易使用。在代码<缓存的< /代码>结束时,您需要停止for循环,或者最好在<代码>缓冲区[i]。='\' 0 ' /COD>也许在单词之间添加一个空格,最后添加最后的<代码> 0“/CODE >。在FGISC或C++中,可以直接使用<代码>数组+ No.Objults>代码,因为C++容器类比MalC/CyrOLC和指针数组/ RealOrror更容易使用。
buffer[i]=='\0'
也许可以在单词之间添加一个空格,最后添加最后的
\0
您可以直接使用fgetstry中的
数组+numberOfChars
来指出操作代码中的错误所在,如果您可以添加更多的解释以使更多人受益……感谢@OlivierPellier Cuit提供的解决方案,它工作得很好!但是当我打印数组时,我得到的结果是:'first two'每行一个字符串,而不是:'first two'一行中的所有字符串。这是一个问题吗?因为我想计算数组的字数这是因为FGET添加了一个行尾字符。您可以删除它,但如果您只想计算字数,这将不会是一个问题。非常感谢您的帮助!尝试指出操作代码中的错误所在,如果您可以添加更多解释以使更多人受益……感谢@OlivierPellier Cuit提供的解决方案,效果很好!但当我打印数组时,我得到的结果是:“前两个”每行一个字符串,而不是“前两个”每行所有字符串都是一个问题吗?因为我想数一次这是因为fgets添加了一个行尾字符。你可以删除它,但如果你只想数一数单词的话就不会有问题了。非常感谢你的帮助!