0

首先,我将向您展示我的代码。

std::ifstream file("accounts/22816.txt");
if(file){
   char *str[50];
   int count=0;
   str[0] = new char[50];
   while(file.getline(str[count], 50)){
      count++;
      str[count] = new char[50];
   }
   for(int i=0;i<count;i++){
      std::cout << str[i] << std::endl;
   }
   delete[] str;  // Here is the problem
}

前面代码的行为是:

  • 逐行读取文本文件的内容。
  • 将每一行保存在二维数组的项目中。
  • 打印二维数组的项目。
  • 最后,从内存 << 中删除数组and this reason of the problem

测试我的应用程序时总是给我运行时错误消息_block_type_is_valid(phead- nblockuse).

我知道这个问题,因为这个delete[] str;

4

1 回答 1

1

str是一个指针数组,每个指针都指向一个动态分配的数组。

您需要遍历它并调用delete []每个元素。

for(int i=0; i < count; ++i){
  delete [] str[i];
}

注意:我已经为 OP 提供了一个使用std::vector,std::stringstd::getline.

于 2013-07-29T05:12:48.680 回答