[英]How to fix segmentation fault from structs in c++?
我正在尝试将文本文件中的每个唯一词写入唯一词结构中,但我还没有那么做,但是在尝试将值写入我的结构数组时遇到了分段错误。
我们还不能使用指针,因为还没有了解它们,我们必须使用命名空间std。
我很好地弄清楚了如何对唯一的单词部分使用循环,我只需要帮助弄清楚如何解决分段错误。 我已经将其范围缩小到了do-while循环中for循环中分段错误的起始位置,但是我不知道从那里开始的错误,而且我找不到任何在线帮助。
#include <iostream>
#include <fstream>
#include <cmath>
#include <string>
using namespace std;
struct UniqueWord{
string word;
int numOccurences;
};
int main(){
const int N = 100000;
ifstream CleanedText;
CleanedText.open("testTextCleaned.txt"); //change to proper text later
string word1;
string words[N];
for(int i = 0; i < N; i++){
CleanedText >> words[i];
}
CleanedText.close();
CleanedText.open("testTextCleaned.txt"); //change to proper text later
UniqueWord wordarray[N];
for(int i = 0; i < N; i++){
CleanedText >> word1;
do{
for(int j = 0;j<N+1;j++){
wordarray[j].word = word1;
}
}while(words[i] != word1);
}
return 0;
}
我希望能够将原始文件中的每个单词放入结构数组中。
在结束条件上失去+1
。
for(int j = 0;j<N/*+1*/;j++){
wordarray[j].word = word1;
}
J需要从0到N-1。 假设N为2,则wordarray [0]和wordarray [1]有效。 wordarray [2]不是。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.