最近设计神经网络,为了方便地从文件(主要是txt)中读取大量的数据数据,我设计了一个类CFileToMatrix。然后设计了一个基于对话框的小程序来测试一下CFileToMatrix可不可以正常工作。这个小程序编译通过,但是在打开文件是提示:debug Error!
Program:...tudio 2008\projects\FileToDlgReader\Debug\FileToDlgReader.exe
HEAP CORRUPTION DETECTED: after Normal block(#110)at 0x00CEA448.
CRT detected that the application wrote to memory after end of heap buffer.我的代码错在什么地方??下面是我的主要代码:///FileToMatrix.h#ifndef FILETOMATRIX_H
#define FILETOMATRIX_H#include "stdafx.h"
#include <fstream>
#include "Vector.h"
#include "Matrix.h"
using namespace std;
using namespace Flood;class CFileToMatrix
{
public:
CFileToMatrix();
CFileToMatrix(CString orignalFile,int numOfColumn);//一般变量个数好确定,样本数比较难!
virtual ~CFileToMatrix(); ////Methods
int GetNumOfRows();
Matrix<double> GetTheMatrix();  //某些行组成的矩阵
private:
CString orignalFile;
int numOfRows;
int numOfColumn;
Matrix<double> matrix;
};
#endif FILETOMATRIX_H
///                          FileToMatrix.cpp
#include "stdafx.h"
#include <fstream>
#include "FileToMatrix.h"using namespace std;
using namespace Flood;//construction/destruction
CFileToMatrix::CFileToMatrix()
{}CFileToMatrix::~CFileToMatrix()
{}CFileToMatrix::CFileToMatrix( CString filename,int ColumnNumber)
{
CString orignalFile=filename;
int numOfRows=0;
int numOfColumn=ColumnNumber;
Matrix<double> matrix(numOfRows,numOfColumn);
}int CFileToMatrix::GetNumOfRows()
{
ifstream file(orignalFile,ios_base::in);
int numy=0;  
char ch[50]; if (file.is_open())
{
while(!file.eof())
{
file.getline(ch, sizeof(ch));
numy++;                       //得到ttp.txt中有多少行数据!
}
numOfRows=numy;
numy=0;
}
file.close();
return numOfRows;
}Matrix<double> CFileToMatrix::GetTheMatrix()
{
char* charFileName=(char*)(LPCTSTR)orignalFile;//CString to char*; ifstream ile(/*orignalFile*/charFileName,ios_base::in);
int numy=GetNumOfRows();

Matrix<double> matrix(numy,numOfColumn);//二维数组 for (int i=0;i<numy;i++)
{
for (int j=0;j<numOfColumn;j++)
{
ile>>matrix[i][j];
}
}
return matrix;
}下面是测试程序的代码片段void CFileToDlgReaderDlg::OnOpenFile()
{
// TODO: Add your control notification handler code here CFileDialog dlg(TRUE, NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
_T("Text Files (*.txt)|*.txt|All Files (*.*)|*.*||"), NULL);
if (dlg.DoModal()==IDOK)
{
CString cstrfilename=dlg.GetPathName();
//char*=(LPCTSTR)cstrfilename;
CFileToMatrix file(cstrfilename,6);
file.GetTheMatrix();
}
}

解决方案 »

  1.   

    内存问题解决啦,问题出在GetTheMatrix()函数中没有ile.close,也就是ifstream()后要close!但是却出现:invalid allocation size:4294967295 bytesF10单步调试,在Matrix <double> matrix(numy,numOfColumn);//二维数组 for (int i=0;i <numy;i++) 

    for (int j=0;j <numOfColumn;j++) 

    ile>>matrix[i][j]; 

    这个循环就调不过去了,这个Matrix<type> 我是用得现成的,问题出在哪儿??
      

  2.   

    ifstream in("test", ios::in | ios::binary); 
      if(!in) { 
        cout << "Cannot open file.\n"; 
        return 1; 
      } 
     
      in.read((char *) &n, sizeof n); 
    参照上面的代码试试,还有,你的数值在txt文件里是怎样存储的
      

  3.   

    我的数据在txt中是这样存储的:
    1.02 1.56 2.23 1.45 1.01
    1.04 1.24 2.01 1.56 1.11
    ...
    类似矩阵吧! 
      

  4.   


    int CFileToMatrix::GetNumOfRows() 

    ifstream file(orignalFile,ios_base::in); 
    int numy=0;  
    char ch[50]; if (file.is_open()) 

    while(!file.eof()) 

    file.getline(ch, sizeof(ch)); 
    numy++;                      //得到ttp.txt中有多少行数据! 

    numOfRows=numy; 
    numy=0; 

    file.close(); 
    return numOfRows;