CString子串问题
我在使用文件 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将
char world[21], data[21], eat[21];
更改为char world[21]
的 strlen 如果未初始化则未定义`Change
char world[21], data[21], eat[21];
tostrlen of
char world[21]
is not defined if its not initialized`好吧,你有几个标准库选项
substr(...) -> http://www.cplusplus.com/reference/string/string/substr/
或
strstr(...) -> http://www.cplusplus.com/reference/clibrary/cstring/strstr/
:)
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/
:)