Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.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++ 根据文件中的行数确定动态数组大小_C++_Arrays - Fatal编程技术网

C++ 根据文件中的行数确定动态数组大小

C++ 根据文件中的行数确定动态数组大小,c++,arrays,C++,Arrays,我已经创建了一个数组,但现在它的大小需要由文件中的行数决定,它可以正常工作,直到我使用for循环将数组元素放入另一个文件中,我得到错误“\u Pnext是0xCCD0”,这是为什么?如果我在for循环中输入一个数字,即30,而不是行数,那么它就可以正常工作 int number_of_lines=0; std::string line2; std::ifstream myfile("Input_files\\Pronunciations.txt"); while (std::getline(my

我已经创建了一个数组,但现在它的大小需要由文件中的行数决定,它可以正常工作,直到我使用for循环将数组元素放入另一个文件中,我得到错误“\u Pnext是0xCCD0”,这是为什么?如果我在for循环中输入一个数字,即30,而不是行数,那么它就可以正常工作

int number_of_lines=0;
std::string line2;
std::ifstream myfile("Input_files\\Pronunciations.txt");
while (std::getline(myfile, line2))
{
    ++number_of_lines;
}
string *hexsource = new string[number_of_lines];
for (int o = 0; o <number_of_lines; ++o)
{
    file2 >> name[o] >> ipaname[o] >> anyway[o];
}
int number_的_行=0;
std::字符串行2;
std::ifstream myfile(“Input_files\\drunations.txt”);
while(std::getline(myfile,line2))
{
++_线的数量;
}
string*hexsource=新字符串[行数];
对于(into=0;o>name[o]>>ipaname[o]>>anyway[o];
}

for(int o=0;o您确定问题在循环中吗?
如果是这样的话,我可以考虑的一个问题是,如果\u行的数量大于name/ipname/的大小,那么它将产生一个问题

你给了这些多大号的

线数的最大值是多少


您需要在代码中提及所有必需的内容

使用
std::vector
。并且不要
新建std::string
,它是一种值类型,请这样使用。
o
hexsource
未使用,并且我们没有
file2
name
ipaname
的声明。它们在这是代码的一部分,然后使用hexsource。如果我在for循环中输入一个数字,即30,而不是行数,它就可以正常工作。行数为什么不起作用?如果我不能为行数301@Znap--什么是
name
ipaname
无论如何
?您只是凭空引入了这些变量。Pl轻松发布一篇文章。你怎么知道这些数组的大小是否正确?很抱歉,我在这里输入了一个非常愚蠢的错误,它不是o。行数的值是302,文件中的文本正在加载到字符串hexsource[number\u of_lines]。如果在for循环中,我说for(int o=0;o<302;++o),如果只将\u行数更改为302,则程序运行良好,也就是说\u行数的值大于302添加一行:“std::coutThanks,此循环对结束行进行了两次计数,但不知道为什么……如果我使用此方法对行数进行计数,则它将工作`myfile.unset(std::ios_base::skipws);无符号行计数=std::计数(std::istream_迭代器(myfile),std::istream_迭代器(),'\n')`
for (int o = 0; o <++number_of_lines; ++o)
for (int o = 0; o < number_of_lines; ++o)