2011-12-26 262 views
1

我想在数组中插入一个新的换行符,这样如果我的数组的字符串长度增加超过了14,这里显示的数组的其他内容将显示在新的在输出控制台中输入。敌人,例如在下面的程序中,我想在第一行和这14个字符后显示“mein hoon don”。我想在输出控制台的新行中显示下一个内容“Don Don Don”。我读到使用\ 0xa(十六进制)和十进制十进制是换行符。但是当我试图在我的代码中使用它们时,我无法产生所需的输出。在字符数组中插入一个新的换行符

# include <iostream.h> 
# include <stdio.h> 
# include <stdlib.h> 
# include <string.h> 

int main() 
{ 
    char abc[40]; 
    strcpy(abc,"mein hoon don."); 
    abc[15]='\10'; 
    abc[16]='\0'; 
    strcat(abc,"Don Don Don"); 
    cout << "value of abc is " << abc; 
    getchar(); 
} 
+0

是的,你可以说,..as我还从来没有这样做之前,但多数民众赞成我的要求:( – Invictus 2011-12-26 17:28:19

+0

\ 0将有效地终止你的字符串,因为它是一个空终止。尝试ABC [15] =' \ r'; abc [16] ='\ n';有很多更好的方法来完成变量,例如使用stringstream ...哦,如果需要十六进制值,则需要编写如下内容:abc [15] = '\ x0a'; – Yaniro 2011-12-26 17:32:15

+0

唯一的问题是我需要确保在15或14之后,或者任何一定数量的字符。该阵列中的其余字符应该在输出控制台中显示时自动占用一个新行 – Invictus 2011-12-26 17:32:44

回答

1

更改:

abc[15] = '\10'; 
abc[16] = '\0'; 

到:

abc[14] = '\n'; 
abc[15] = '\0'; 
+0

谢谢你..我很抱歉,由于错误的指数计算,我造成了不便。 – Invictus 2011-12-26 17:43:06

+0

不需要道歉。 – hmjd 2011-12-26 17:44:26

1

转义序列\10不符合您的想法。可能最容易使用\n代替。

注:你也可以使用strcat插入新行并防止任何指标计算题:

strcpy(abc,"mein hoon don."); 
/* abc[15]='\10'; */ 
/* abc[16]='\0'; */ 
strcat(abc, "\n"); 
strcat(abc,"Don Don Don"); 

你真是最好使用的std::string代替char阵列:

#include <iostream> 
#include <string> 
int main() { 
    std::string s; 
    s = "mein hoon don."; 
    s += "\n"; 
    s += "Don Don Don"; 
    std::cout << value of abc is " << s << std::endl; 
    return 0; 
} 
+0

使用\ n在abc中[15] ='\ n'不符合我的要求 – Invictus 2011-12-26 17:36:07

+0

为什么不呢?它不工作,或者你需要使用别的东西? – Yaniro 2011-12-26 17:37:19

+0

在这种情况下,值在控制台中显示为“mein hoon don.Don Don Don”,我不希望那个 – Invictus 2011-12-26 17:37:30

1

好了,逃避secuence取决于你正在使用的操作系统:有两个caracters \ R(载体回报)和\ n(换行),在Windows中,您需要同时,在Linux中,您只需要\ r,而在Mac中,您需要\ n。

但是,只要你使用的CPP,你不应该使用其中任何一个,你应该使用endln:

//# include <stdio.h> 
//# include <stdlib.h> 
//# include <string.h> 

#include <ostream> 
#include <iostream> 
#include <sstream> 
#include <string> 

int main() 
{ 
    //char abc[40]; 
    std::ostringstream auxStr; 

    auxStr << "mein hoon don." << std::endl << "Don Don Don" << std::endl; 

    //if you need a string, then: 
    //std::string abc = auxStr.str(); 

    std::cout << "value of abc is " << auxStr.str(); 
    getchar(); 
} 
+0

感谢Hiperion的建议......但我需要将此字符数组传递给参数类型为char *的函数。在我的实际代码中,我把它们放在char * abc中,并通过新的分配大小。 – Invictus 2011-12-26 18:17:31

+0

那么,你只需要做: strncpy(abc,auxStr.str()。c_str(),39); abc [40] = 0; – Hiperion 2011-12-27 08:21:19

0

此代码引入的位置x的断行状态。

只需设置Line_Break=14;即可完成。

# include <iostream.h> 
# include <stdio.h> 
# include <stdlib.h> 
# include <string.h> 

int main() 
{ 
    int LinePos=0; 
    int Line_Break_Pos=0; 
    int Line_Break=10; 
    char abc[40]={""}; 

    strcpy(abc,"mein hoon don. "); 
    strcat(abc,"Don Don Don"); 

    cout << "\n" ; 
    cout << "Without linebreak : \n"; 
    cout << abc ; 
    cout << "\n\n" ; 
    cout << "With linebreak : \n" ; 


    while (LinePos < strlen(abc)) 
    { 
     cout <<abc[LinePos]; 
     if (Line_Break_Pos > Line_Break && abc[LinePos ]==' ') 
     { 
      cout << "\n" ; 
      Line_Break_Pos=0; 
     } 

     LinePos++; 
     Line_Break_Pos++; 
    } 


    cout << "\n\n" ; 
    return 0; 
} 
相关问题