C++ 这是获取C+中字典文件中第一个单词(或任何单词)的字符数的正确方法吗+;?
此代码可编译,但不显示任何内容。我正在读取整个文件,这样我就可以得到列表中任何单词的字符数。我想看看dictionary.txt中每个单词末尾的额外字符是否真的是空格、换行符,或者是什么。为什么它没有显示任何内容?我的笔记本电脑只有4GB内存,这可能是内存问题吗C++ 这是获取C+中字典文件中第一个单词(或任何单词)的字符数的正确方法吗+;?,c++,file,C++,File,此代码可编译,但不显示任何内容。我正在读取整个文件,这样我就可以得到列表中任何单词的字符数。我想看看dictionary.txt中每个单词末尾的额外字符是否真的是空格、换行符,或者是什么。为什么它没有显示任何内容?我的笔记本电脑只有4GB内存,这可能是内存问题吗 //dictionary.txt is found here: https://dev.intentionrepeater.com/cpp/dictionary.txt #include <stdio.h> #includ
//dictionary.txt is found here: https://dev.intentionrepeater.com/cpp/dictionary.txt
#include <stdio.h>
#include <string>
#include <iostream>
#include <fstream>
#define SIZE_OF_WORD_LIST 49528
using namespace std;
int main() {
std::string word_list[SIZE_OF_WORD_LIST-1];
int i;
ifstream file("dictionary.txt");
try
{
if (file.is_open()) {
for (i = 0; i < SIZE_OF_WORD_LIST; ++i) {
file >> word_list[i];
}
}
}
catch (int e)
{
cout << "Error opening file: " << e << endl;
exit(0);
}
cout << "Number of characters in first word: " << std::to_string(word_list[0].length()) << endl;
return 0;
}
//dictionary.txt可在以下位置找到:https://dev.intentionrepeater.com/cpp/dictionary.txt
#包括
#包括
#包括
#包括
#定义单词列表的大小49528
使用名称空间std;
int main(){
std::字符串单词列表[单词列表的大小-1];
int i;
ifstream文件(“dictionary.txt”);
尝试
{
if(file.is_open()){
对于(i=0;i<单词列表的大小;++i){
文件>>单词列表[i];
}
}
}
捕获(INTE)
{
C++中的CUT必须在声明数组时指定元素的数量,而不是最大索引。
您只分配了单词列表-1的SIZE\u
元素,但将其读取到单词列表的SIZE\u
单词
通过访问不存在的元素word\u list[SIZE\u OF\u word\u list-1]
调用的分段错误似乎阻止了它的打印
要避免这种情况,请分配足够的元素
std::string word_list[SIZE_OF_WORD_LIST];
而不是
std::string word_list[SIZE_OF_WORD_LIST-1];
<>在C++中,当声明数组时,必须指定元素的数量,而不是最大索引。
您只分配了单词列表-1的SIZE\u
元素,但将其读取到单词列表的SIZE\u
单词
通过访问不存在的元素word\u list[SIZE\u OF\u word\u list-1]
调用的分段错误似乎阻止了它的打印
要避免这种情况,请分配足够的元素
std::string word_list[SIZE_OF_WORD_LIST];
而不是
std::string word_list[SIZE_OF_WORD_LIST-1];
您只分配了SIZE\u OF theu WORD\u LIST-1
元素,但将其读取到SIZE\u OF theu WORD\u LIST
单词..您只分配了SIZE\u OF theu WORD\u LIST-1
元素,但将其读取到SIZE\u OF theu words\u LIST
单词..行:for(i=0;i