C ++ strtok和2d数组。(C++ strtok and 2d arrays. Program compiles but crashes)

编程入门 行业动态 更新时间:2024-10-23 10:27:45
C ++ strtok和2d数组。(C++ strtok and 2d arrays. Program compiles but crashes) #include <iostream> #using namespace std; #include <cstring> #include <fstream> int main(int argc, char *argv[] ) { ifstream file; // check for valid user input if (argc != 2) { cout << "Please enter the file name." << endl; return 0; } else { file.open (argv[1]); // check if file is valid if (file == 0) { cout << "File is not valid." << endl; return 0; } } char words[100][16]; // 2d array for unique words char line[100]; // c-string to hold a line char *token = NULL; int h=0; // variable for counting array index int i, j; // counter variables while (file.getline (line, 100, '\n')) { bool unique = true; // boolian to test for unique words cout << line << endl; // echo print the line token = strtok(line, " ."); // 1st token if (token == NULL) // check if 1st token exists { break; } // loop to check if token is unique for (i = 0; i < 100; i++) { if (strcmp(token, words[i]) == 0) { unique = false; break; } } if (unique == true) { strcpy(words[h], token); h++; } unique = false; // another loop to continue strtok and check for unique words while (token != NULL) { token = strtok(NULL, " ."); for (i = 0; i < 100; i++) { if (strcmp(token, words[i]) == 0) { unique = false; break; } } if (unique == true) { strcpy(words[h], token); h++; } } } return 0; }

这是我的代码到目前为止,我的所有字符都应该适合数组,并且循环似乎在逻辑上是合理的。 我不明白为什么我的程序编译但运行不正确。 我猜它可能与二维数组和我选择用于strcmp和strcpy的语法有关。 但我尝试用单词[h] [0]代替单词[h],但这也不起作用。 我在这里完全失去了,请帮忙!

#include <iostream> #using namespace std; #include <cstring> #include <fstream> int main(int argc, char *argv[] ) { ifstream file; // check for valid user input if (argc != 2) { cout << "Please enter the file name." << endl; return 0; } else { file.open (argv[1]); // check if file is valid if (file == 0) { cout << "File is not valid." << endl; return 0; } } char words[100][16]; // 2d array for unique words char line[100]; // c-string to hold a line char *token = NULL; int h=0; // variable for counting array index int i, j; // counter variables while (file.getline (line, 100, '\n')) { bool unique = true; // boolian to test for unique words cout << line << endl; // echo print the line token = strtok(line, " ."); // 1st token if (token == NULL) // check if 1st token exists { break; } // loop to check if token is unique for (i = 0; i < 100; i++) { if (strcmp(token, words[i]) == 0) { unique = false; break; } } if (unique == true) { strcpy(words[h], token); h++; } unique = false; // another loop to continue strtok and check for unique words while (token != NULL) { token = strtok(NULL, " ."); for (i = 0; i < 100; i++) { if (strcmp(token, words[i]) == 0) { unique = false; break; } } if (unique == true) { strcpy(words[h], token); h++; } } } return 0; }

This is my code so far, all my characters should fit the array and the loops appear to be logically sound. I don't understand why my program compiles but doesn't run correctly. I am guessing that it might have something to do with 2 dimensional arrays and the syntax I chose to use for strcmp and strcpy. But I tried putting words[h][0] instead of words[h] and that doesn't work either. I'm kind of at a complete loss here, please help!

最满意答案

首先,您必须对数组进行零初始化

char words[100][16] = {};

这个循环

for (i = 0; i < 100; i++) { if (strcmp(token, words[i]) == 0) { unique = false; break; } }

应该改为

for (i = 0; i < h; i++) { if (strcmp(token, words[i]) == 0) { unique = false; break; } }

此代码段

while (token != NULL) { token = strtok(NULL, " ."); // ...

必须替代

while ( ( token = strtok(NULL, " .") ) != NULL) { // ...

我认为这是程序崩溃的主要原因。

此外,您不会检查文件中是否有比单元大小更多的唯一单词以及令牌的大小是否大于数组的第二个大小。

First of all you have to zero-initialize the array

char words[100][16] = {};

This loop

for (i = 0; i < 100; i++) { if (strcmp(token, words[i]) == 0) { unique = false; break; } }

should be changed to

for (i = 0; i < h; i++) { if (strcmp(token, words[i]) == 0) { unique = false; break; } }

This code snippet

while (token != NULL) { token = strtok(NULL, " ."); // ...

has to be substituted for

while ( ( token = strtok(NULL, " .") ) != NULL) { // ...

I think that it is the main reason of the program crash.

Also you do no check whether there are more unique words in the file than the size of the array and whether the size of a token is greater than the second size of the array.

更多推荐

本文发布于:2023-07-28 01:15:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1298280.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   arrays   strtok   crashes   compiles

发布评论

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

>www.elefans.com

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