CString子串问题

发布于 2024-11-10 13:23:18 字数 1718 浏览 7 评论 0原文

我在使用文件 i/o 实现的子字符串方面遇到问题。我有 char data[21]、char world[21] 和 char eat[21],它们将是 char in[300] 的子字符串(顺便说一句,这些名称没有任何意义)。我想出了一些子字符串代码,但我没有得到我想要的子字符串,而是得到了整个字符串和一些时髦的字符。有人可以告诉我我做错了什么,或者如果我只是在过去的几个小时内省去了时间并且库中已经有一个子 Cstring 方法?

int main()
{
    char in[300] = "w54;d68;n541;"  // this is bigger than it needs to be because its for file i/o
    char world[21], data[21], eat[21];
    int w, d, n, end1, end2, end3;

    for (w = 0; in[w] != 'w'; w++) {
    }

    for (end1 = w; in[end1] != ';'; end1++) {
    }

    d = end1 + 1;

    for (end2 = d; in[end2] != ';'; end2++) {
    }

    n = end2 + 1;

    for (end3 = n; in[end3] != ';'; end3++) {
    }

    int i;

    for (i = w + 1; i < end1; i++) {
      append(world, in[i]);
    }

    for (i = d + 1; i < end2; i++) {
      append(data, in[i]);
    }

    for (i = n + 1; i < end3; i++) {
      append(eat, in[i]);
    }

    cout << "world[21]: " << world << endl << "data[21]: " << data << endl << "eat[21]: " << eat << endl;
}

void append(char *s, char c)
{
    int len = strlen(s);
    s[len] = c;
    s[len + 1] = '\0';
}

这是我的断点(就在 return 0 之前)结果

in  0x0021fbb8 "w54;d68;n541;5468541"   char [300]
end2    7   int
world   0x0021fb98 "ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌw54;d68;n541;5468541"   char [21]
data    0x0021fb78 "ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌw54;d68;n541;5468541"   char [21]
eat 0x0021fb58 "ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌw54;d68;n541;5468541"   char [21]

n   8   int
i   12  int
end3    12  int
w   0   int
end1    3   int

I am having trouble with substrings that I will implement with file i/o. I have char data[21],char world[21], and char eat[21] that will be substrings of char in[300] (the names dont mean anything btw). I came up with some substring code and instead of getting the substring I wanted, I get the whole string and some funky characters. Can someone tell me what I'm doing wrong, or if I just waisted the past couple hours and there already is a sub-Cstring method in the library?

int main()
{
    char in[300] = "w54;d68;n541;"  // this is bigger than it needs to be because its for file i/o
    char world[21], data[21], eat[21];
    int w, d, n, end1, end2, end3;

    for (w = 0; in[w] != 'w'; w++) {
    }

    for (end1 = w; in[end1] != ';'; end1++) {
    }

    d = end1 + 1;

    for (end2 = d; in[end2] != ';'; end2++) {
    }

    n = end2 + 1;

    for (end3 = n; in[end3] != ';'; end3++) {
    }

    int i;

    for (i = w + 1; i < end1; i++) {
      append(world, in[i]);
    }

    for (i = d + 1; i < end2; i++) {
      append(data, in[i]);
    }

    for (i = n + 1; i < end3; i++) {
      append(eat, in[i]);
    }

    cout << "world[21]: " << world << endl << "data[21]: " << data << endl << "eat[21]: " << eat << endl;
}

void append(char *s, char c)
{
    int len = strlen(s);
    s[len] = c;
    s[len + 1] = '\0';
}

and here are my breakpoint (right before return 0) results

in  0x0021fbb8 "w54;d68;n541;5468541"   char [300]
end2    7   int
world   0x0021fb98 "ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌw54;d68;n541;5468541"   char [21]
data    0x0021fb78 "ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌw54;d68;n541;5468541"   char [21]
eat 0x0021fb58 "ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌw54;d68;n541;5468541"   char [21]

n   8   int
i   12  int
end3    12  int
w   0   int
end1    3   int

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荒人说梦 2024-11-17 13:23:19

char world[21], data[21], eat[21]; 更改为

char world[21] = {0};
char data[21] = {0};
char eat[21] = {0};

char world[21] 的 strlen 如果未初始化则未定义`

Change char world[21], data[21], eat[21]; to

char world[21] = {0};
char data[21] = {0};
char eat[21] = {0};

strlen of char world[21] is not defined if its not initialized`

累赘 2024-11-17 13:23:19

Well you have a couple of standard library options

substr(...) -> http://www.cplusplus.com/reference/string/string/substr/

or

strstr(...) -> http://www.cplusplus.com/reference/clibrary/cstring/strstr/

:)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文