|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
下面是的代码目的是从一个TEXT文件中读取未知大小的矩阵,并将矩阵存放在一个动态分配的空间中。我在用VS2013调试的时候,在红色代码处出现了缓冲区溢出,谁能帮我解释一下为什么码?
- // 从文件读入矩阵
- Matrix Matrix::MatValueFileIn(const char *infilename="matrixin.txt")
- {
- ifstream inFile;
- inFile.open(infilename,ios::in);
- int row=0; //矩阵行
- int col=0; //矩阵列
- string temp;
- double datatemp=0.0;
- vector<double> veMatData; //用来保存输入的临时数据
- if(inFile.bad())
- {
- cout<<"文件不存在"<<endl;
- exit(1);
- }
- if(inFile.is_open())
- {
- cout<<"文件打开成功"<<endl;
- while(getline(inFile,temp,'\n'))
- {
- ++row; //获得矩阵行数
- }
- temp.clear();
- inFile.clear();
- inFile.seekg(0); //重新定位到文件头
- {
- double m; //获得矩阵列
- char c;
- while(inFile>>m)
- {
- ++col;
- c=inFile.peek();
- if(c=='\n')
- {
- break;
- }
- }
- }
- inFile.clear();
- inFile.seekg(0);
- this->matRow=row;
- this->matCol=col;
- this->pMatValue=new double[row*col];
-
- while(inFile.good() && !inFile.eof())
- {
-
- inFile>>datatemp;
- veMatData.push_back(datatemp); //将矩阵数据读入到vector中
- }
- if(!veMatData.empty())
- {
- for(size_t i=0; i <(veMatData.size())-1;i++)
- {
- [color=Red]this->pMatValue[i]=veMatData[i];[/color]
- }
- }
- inFile.clear();
- inFile.close();
- }
- else
- {
- cerr<<"文件打开失败"<<endl;
- exit(0);
- }
- return *this;
- }
复制代码 |
|