Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何修复strtok编译器错误?_C++_C - Fatal编程技术网

C++ 如何修复strtok编译器错误?

C++ 如何修复strtok编译器错误?,c++,c,C++,C,大家好,我写这段代码是为了查看文本文件,并将找到的每个单词放入c字符串数组中。我能够编写代码,但当实际文本文件中出现错误时,我会遇到问题。例如,我的程序会崩溃,如果在句子中有两个空格,比如“汽车开得很快”,它会停在汽车旁边。看看我的代码,我相信这是因为strtok。我认为要解决这个问题,我需要让strtok为下一个值做一个标记,但我不知道如何做 我的代码 #include <iostream> #include <fstream> #include <string.

大家好,我写这段代码是为了查看文本文件,并将找到的每个单词放入c字符串数组中。我能够编写代码,但当实际文本文件中出现错误时,我会遇到问题。例如,我的程序会崩溃,如果在句子中有两个空格,比如“汽车开得很快”,它会停在汽车旁边。看看我的代码,我相信这是因为strtok。我认为要解决这个问题,我需要让strtok为下一个值做一个标记,但我不知道如何做

我的代码

#include <iostream>
#include <fstream>
#include <string.h>
#include <stdlib.h>
using namespace std;

int main() {
   ifstream file;
   file.open("text.txt");
   string line;

   char * wordList[10000];
   int x=0;

   while (getline(file,line)){

      // initialize a sentence
      char *sentence = (char*) malloc(sizeof(char)*line.length());
      strcpy(sentence,line.c_str());

      // intialize a pointer
     char* word;

      // this gives us a pointer to the first instance of a space, comma, etc.,   
      // that is, the characters in "sentence" will be read into "word" 
      // until it reaches   one of the token characters (space, comma, etc.)
     word = strtok(sentence, " ,!;:.?");

      // now we can utilize a while loop, so every time the sentence comes to a new 
      // token character, it stops, and "word" will equal the characters from the last
      // token character to the new character, giving you each word in the sentence

      while (NULL != word){
         wordList[x]=word;
         printf("%s\n", wordList[x]);
         x++;
         word = strtok(NULL," ,!;:.?");
      }
    }
    printf("done");
    return 0;
}   
#包括
#包括
#包括
#包括
使用名称空间std;
int main(){
ifstream文件;
打开(“text.txt”);
弦线;
字符*字表[10000];
int x=0;
while(getline(文件,行)){
//初始化一个句子
char*语句=(char*)malloc(sizeof(char)*line.length());
strcpy(句子,第行c_str());
//初始化指针
字符*字;
//这给了我们一个指向空格、逗号等的第一个实例的指针。,
//也就是说,“句子”中的字符将被读入“单词”
//直到到达其中一个标记字符(空格、逗号等)
单词=strtok(句子,,!;:?);
//现在我们可以利用while循环,所以每次句子都是新的
//标记字符,它停止,并且“word”将等于最后一个字符中的字符
//标记字符到新字符,给出句子中的每个单词
while(NULL!=word){
单词表[x]=单词;
printf(“%s\n”,单词列表[x]);
x++;
word=strtok(空,,!;:.?”;
}
}
printf(“完成”);
返回0;
}   

<> P>我知道有些代码是C++的,有些是C的,但是我想在C

中做最多的事情。问题可能是你没有为空的终止字符串分配足够的空间。
  char *sentence = (char*) malloc(sizeof(char)*line.length());
  strcpy(sentence,line.c_str());
如果需要捕获
“abc”
,则字符需要3个元素,终止空字符需要另一个元素,即总共4个字符

malloc
的参数值需要增加1

  char *sentence = (char*) malloc(line.length()+1);
  strcpy(sentence,line.c_str());
不清楚为什么在C++程序中使用<代码> MalOC/<代码>。我建议使用
new

  char *sentence = new char[line.length()+1];
  strcpy(sentence,line.c_str());

教授混合和匹配C++和C,但是它应该是C类。所以我现在对C++没有太多的了解。亚历克斯,尽量坚持一个,如果你的教授不介意你使用哪一个。我实际上测试了应该读取的文件,问题是当它应该以双空格或两个单词之间只有一个句点的连续顺序变为null ass的值时,会出现故障。@R Sahu我真的很讨厌我必须这样做,但是教授把他教的东西和他在例子中给我们的东西翻过来。我认为他的大多数例子都是C++的。