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

在一个函数中用键盘输入函数名,然后通过参数将这个函数名传递到另一个函数,在另一个函数中怎么就打不开这个文件呢?

hsnr 发布于 2010-05-16 06:19, 695 次点击
在一个函数中用键盘输入函数名,然后通过参数将函数名传递到另一个函数,在另一个函数中怎么就打不开这个文件呢?请问错在哪?程序如下。
// Input all basic data of this c++ program

void InputBasic(string& IfStr,string& OfStr)
{
    ifstream inputfile;      
    cout<<endl;
    cout<<"请输入第一个基本数据(输入)文件名"<<endl;
    cin>>IfStr;    //键盘输入输入数据文件名
    cout<<endl;
    inputfile.open(IfStr.c_str());  //打开这个文件,可以打开
    if(inputfile.fail())
    {
        cout<<"第一个基本数据(输入)文件无法打开"<<endl;
        cout<<endl;
        exit(1);
    }

//
    ofstream outputfile;
    cout<<"请输入第一个基本数据(输出)文件名"<<endl;
    cin>>OfStr;  //键盘输入输出数据文件名
    cout<<endl;
    outputfile.open(OfStr.c_str());  //打开文件,可以打开
    if(outputfile.fail())
    {
        cout<<"第一个基本数据(输出)文件无法打开"<<endl;
        cout<<endl;
        exit(2);
    }
   ...............

    inputfile.close();
    outputfile.close();
    return;
}

void InputBasicData(string& IfStr,string& OfStr)
{
    ifstream inputfile;
    cout<<IfStr.c_str()<<"  "<<OfStr.c_str()<<endl;//可以输出传入的两个字符串
    inputfile.open(IfStr.c_str(),ios_base::app);      //执行到这里显示:"基本数据(输出)文件无法打开"
    inputfile.seekg(8*sizeof(int)+2*sizeof(double), ios::beg);
    if(inputfile.fail())
    {
        cout<<"基本数据(输入)文件无法打开"<<endl;
        exit(2);
    }
//
    ofstream outputfile;
    outputfile.open(OfStr.c_str(),ios::app);
    inputfile.seekg(8*sizeof(int)+2*sizeof(double), ios::beg);
    if(outputfile.fail())    {
        cout<<"基本数据(输出)文件无法打开"<<endl;
        exit(2);
    }
    ..............

    inputfile.close();
    outputfile.close();

    return;
}
int _tmain(int argc, _TCHAR* argv[])
{
    string IfStr,OfStr;
    data.InputBasic(IfStr,OfStr);
    cout<<IfStr<<OfStr<<endl;// 可以输出
    data.InputBasicData(IfStr,OfStr);
   ................
    return 0;
}
1 回复
#2
hsnr2010-05-17 09:15
没有人可以解答这个问题啊?
1