我试图标记一个字符串并插入一个标记作为键,其余作为映射的值。 但插入时,我遇到了分段错误。 我调试了很长时间但无法找到克服此错误的方法。 这是我的代码:
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; } }更多推荐
发布评论