【问题标题】:string does not end with null字符串不以 null 结尾
【发布时间】:2013-12-02 20:24:56
【问题描述】:

我是 C++ 的初学者,我遇到了以下代码的问题:

我正在尝试将“新行”或"\n" 连接到 char 矩阵中的字符串。 到目前为止,我设法连接了一个 " " 字符,但字符 "\n" 或仅输入多个 " " 将不起作用。

实际示例为我定义的 3 个矩阵中的每一个获取 3 个 const 值为 10(最大字符)的字符串 - 将值分配给前两个,并使用函数“更改”第三个并打印它。

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

const int LINES = 3;
const int MAXCHARS = 10; //TO DO: change to 81 for final version

void cpyAndCat(char[][MAXCHARS], char[][MAXCHARS], char[][MAXCHARS], int);
void main()
{
    char text1[LINES][MAXCHARS], text2[LINES][MAXCHARS], text3[LINES][MAXCHARS];

    cout << "Enter " << LINES << " lines into text1:\n";
    for (int i = 0; i < LINES; i++) // assign the matrix of chars text1 with strings
    {
        _flushall();
        cin.getline(text1[i], MAXCHARS);
    }
    cout << "Enter " << LINES << " lines into text2: \n";
    for (int i = 0; i < LINES; i++) // assign the matrix of chars text2 with strings
    {
        _flushall();
        cin.getline(text2[i], MAXCHARS);
    }
    //TO DO: call the function which will recieve text1 and text2 
    //and put blank line(line too long) or copied line from text1 and catanted line form text2.(long correct size)
    cpyAndCat(text1, text2, text3, LINES);
    cout << "============================================================\n";
    for (int i = 0; i < LINES; i++) // print third matrix of chars, prints 3 lines of either text or '\n'
    {
        _flushall();
        cout << text3[i];
        cout << endl;
    }

    system("pause");

}
void cpyAndCat(char text1[][MAXCHARS], char text2[][MAXCHARS], char text3[][MAXCHARS], int lines)
{

    for (int i = 0; i < lines; i++) // searches if length of string from first 2 matrix is valid
    {
        if (strlen(text1[i]) + strlen(text2[i]) < MAXCHARS) // if so, copy the first to the third and catanate the second to the third
        {
            strcpy_s(text3[i], text1[i]);
            strcat_s(text3[i], text2[i]);
        }
        else // if else (: , catanate 'new line' to the third matrix
        {
            strcat_s(text3[i], "\n"); // not working                                                                           
        }
        cout << endl;
    }
}

【问题讨论】:

  • 请发布您尝试过的真实代码。 strcat_s(text3[i],"\n"// not working 是一个明显的语法错误,从您的其余代码来看,您可能并没有真正使用它。
  • 只需使用std::string。真的,它会让一切变得更容易和更好。
  • 对不起,我不明白,那是我尝试过的真实代码。以及我想在这里弄清楚的内容-我应该放什么而不是“\ n”字符。 Alan Stokes-你说的功能/方法是什么,还没学过..:/
  • 如果您的书告诉您void main() 是正确的,那么它是由不懂该语言的人编写的。 int main() 是正确的定义。
  • @user2176127:不正确。 system 函数在 &lt;cstdlib&gt;&lt;stdlib.h&gt; 中声明。 "PAUSE" 只是一个字符串文字(在运行时解析为命令名称)。 system("PAUSE") 不需要 &lt;windows.h&gt;

标签: c++ string function concatenation


【解决方案1】:

strcat_sstrcpy_s 需要 three parameters, not two。我很惊讶你有任何东西在编译。

另外,你 strcattext3 没有初始化它。所以这可能是未定义的行为......

【讨论】:

  • 我不想使用 strcat_s 但编译器一直告诉我 strcat 很危险,因为它弃用了某些东西......你会怎么做?我被告知 text3 必须以垃圾开头
  • @e-Lay:编译器有点意思,但std::string 是更好的解决方案。要连接两个字符串,您可以使用+
  • @e-Lay:你用的是微软的编译器,对吧?就 C 标准而言,strcat 并没有被“弃用”,如果你小心的话,它可以安全地使用。 strcat_sstrcat 的特定于 Microsoft 的“更安全”版本;使用它可能会使你的代码更安全,但它也降低了它的可移植性。 (2011 年的 C 标准添加了 strcat_s,作为 可选 边界检查接口的一部分;据我所知,C++ 没有添加它。)
【解决方案2】:

strcat_s 接受 3 个参数,您缺少以字节为单位的大小,而且该字符串从未初始化。

您可能希望复制到字符串中,然后在需要时进行连接。 不要忘记考虑每个字符串末尾的“\0”。

strcpy_s(text3[i], 2, "\n");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-11
    • 2011-05-01
    相关资源
    最近更新 更多