注册 登录
编程论坛 C++教室

txt读取及二维动态数组

y605302737 发布于 2013-04-18 19:41, 566 次点击
大家好,能不能帮我看下这个程序哪错了。运用后显示:Can't open the file yyy.txt
                                                  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这个数组吗??
3 回复
#2
azzbcc2013-04-18 19:53
ifstream fin1;
fin1.open( s1.c_str() );
#3
y6053027372013-04-18 20:51
回复 2楼 azzbcc
按照您的回答改对了,我看了下原因,是要提供一个C-风格的字符串参数,这是什么什么意思,是不是open()的参数只能是char[]数组这种类型的。谢谢!!
#4
azzbcc2013-04-18 21:21
自己看看open的参数不就都清楚了,,,
1