![]() |
#2
azzbcc2013-04-18 19:53
|
End of the file
yyy.txt为:1.1 2.1 3.1
3.1 2.1 1.1

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int Gamma_row = 2;
const int Gamma_arr = 3;
float ** Conv(string &s1,const int row , const int arr);
int main()
{
string str = "yyy.txt";
float ** Gamma1;
Gamma1 = Conv(str , Gamma_row , Gamma_arr);
cout<<Gamma1<<endl;
for(int i=0;i!=Gamma_row;i++)
{
delete [] Gamma1[i];
}
delete [] Gamma1;
return 0;
}
float ** Conv(string &s1, const int row, const int arr)
{
float ** array = NULL;
array = new float *[row];
for(int i=0;i!=row;i++)
{
array[i] = new float [arr];
}
ifstream fin1;
fin1.open("s1");
if(!fin1.is_open())
{
cout<<"Can't open the file "<<s1<<endl;
fin1.clear();
}
float content;
for(int i = 0; i != row*arr; i++)
{
fin1 >> content;
array[i/arr][i%row] = content;
}
if(fin1.eof())
{
cout<<"End of the file "<<endl;
fin1.clear();
}
fin1.close();
return array;
}
另外还想问下,动态数组释放后还能输出Gamma1这个数组吗?? #include <fstream>
#include <string>
using namespace std;
const int Gamma_row = 2;
const int Gamma_arr = 3;
float ** Conv(string &s1,const int row , const int arr);
int main()
{
string str = "yyy.txt";
float ** Gamma1;
Gamma1 = Conv(str , Gamma_row , Gamma_arr);
cout<<Gamma1<<endl;
for(int i=0;i!=Gamma_row;i++)
{
delete [] Gamma1[i];
}
delete [] Gamma1;
return 0;
}
float ** Conv(string &s1, const int row, const int arr)
{
float ** array = NULL;
array = new float *[row];
for(int i=0;i!=row;i++)
{
array[i] = new float [arr];
}
ifstream fin1;
fin1.open("s1");
if(!fin1.is_open())
{
cout<<"Can't open the file "<<s1<<endl;
fin1.clear();
}
float content;
for(int i = 0; i != row*arr; i++)
{
fin1 >> content;
array[i/arr][i%row] = content;
}
if(fin1.eof())
{
cout<<"End of the file "<<endl;
fin1.clear();
}
fin1.close();
return array;
}