标记字符串时出现分段错误(Segmentation fault while tokenizing string)

编程入门 行业动态 更新时间:2024-10-25 12:19:28
标记字符串时出现分段错误(Segmentation fault while tokenizing string)

我试图标记一个字符串并插入一个标记作为键,其余作为映射的值。 但插入时,我遇到了分段错误。 我调试了很长时间但无法找到克服此错误的方法。 这是我的代码:

while (!fin.eof()) { char *str1; char buf[MAX_CHARS_PER_LINE]; fin.getline(buf, MAX_CHARS_PER_LINE); str1 = new char[strlen(buf)]; int n = 0; char *token[MAX_TOKENS_PER_LINE] = {}; token[0] = strtok(buf, DELIMITER); if (token[0]) { for (n = 1; n < MAX_TOKENS_PER_LINE; n++) { token[n] = strtok(0, DELIMITER); if (!token[n]) break; } } // Forming str1 using the tokens here strcpy(str1, token[0]); strcat(str1, ":"); strcat(str1, token[1]); int key = atoi(token[3]); // Adding str1 to map nameId[key] = str1; } }

任何帮助,将不胜感激!

I am trying to tokenize a string and insert one token as the key and the rest as values of a map. But while inserting, I get a segmentation fault. I debugged for a long time but could not find a way to overcome this error. Here is my code:

while (!fin.eof()) { char *str1; char buf[MAX_CHARS_PER_LINE]; fin.getline(buf, MAX_CHARS_PER_LINE); str1 = new char[strlen(buf)]; int n = 0; char *token[MAX_TOKENS_PER_LINE] = {}; token[0] = strtok(buf, DELIMITER); if (token[0]) { for (n = 1; n < MAX_TOKENS_PER_LINE; n++) { token[n] = strtok(0, DELIMITER); if (!token[n]) break; } } // Forming str1 using the tokens here strcpy(str1, token[0]); strcat(str1, ":"); strcat(str1, token[1]); int key = atoi(token[3]); // Adding str1 to map nameId[key] = str1; } }

Any help would be appreciated!

最满意答案

经过进一步的调试,我发现了确切的问题。 在将它们与str1连接之前,我没有检查令牌是否为NULL 。 在我强制执行该检查后,str1总是获得一个有效值,因此插入到地图中。

这是我更新的代码:

while (!fin.eof()) { char *str1; char buf[MAX_CHARS_PER_LINE]; fin.getline(buf, MAX_CHARS_PER_LINE); str1 = new char[strlen(buf)]; int n = 0; char *token[MAX_TOKENS_PER_LINE] = {}; // Enforcing NULL check for buf if (buf) { token[0] = strtok(buf, DELIMITER); } // Enforcing the NULL check for the tokens if (token[0]) { for (n = 1; n < MAX_TOKENS_PER_LINE; n++) { token[n] = strtok(0, DELIMITER); if (!token[n]) break; } pair<map<int, char *>::iterator, bool> result; // Forming str1 using the tokens here strcpy(str1, token[0]); strcat(str1, ":"); strcat(str1, token[1]); // Adding str1 to map int key = atoi(token[3]); nameId[key] = str1; } }

After further debugging, I figured out the exact problem. I did not check if the tokens were NULL before concatenating them with str1. After I enforced that check, str1 always gets a valid value and hence, gets inserted in the map.

Here is my updated code:

while (!fin.eof()) { char *str1; char buf[MAX_CHARS_PER_LINE]; fin.getline(buf, MAX_CHARS_PER_LINE); str1 = new char[strlen(buf)]; int n = 0; char *token[MAX_TOKENS_PER_LINE] = {}; // Enforcing NULL check for buf if (buf) { token[0] = strtok(buf, DELIMITER); } // Enforcing the NULL check for the tokens if (token[0]) { for (n = 1; n < MAX_TOKENS_PER_LINE; n++) { token[n] = strtok(0, DELIMITER); if (!token[n]) break; } pair<map<int, char *>::iterator, bool> result; // Forming str1 using the tokens here strcpy(str1, token[0]); strcat(str1, ":"); strcat(str1, token[1]); // Adding str1 to map int key = atoi(token[3]); nameId[key] = str1; } }

更多推荐

本文发布于:2023-04-27 23:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329202.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   标记   错误   Segmentation   tokenizing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!