C++ 无法使用ifstream打开文件

C++ 无法使用ifstream打开文件,c++,C++,我想得到一些关于以下代码行的帮助。 对于constructArray函数,我无法按如下所示运行它!一个文件作为真实的信息,但我不知道是什么错误。 非常感谢你的帮助 还有如何使用.txt创建infle文件名,我已经尝试使用+“.txt”缩进,但由于参数中的文件类型,我无法这样做。 编译器运行映像: 使用名称空间std; 枚举NumType{奇数,偶数}; 结构编号 { 国际贸易编号; 核型型; 整数位数; 整数位数; 整数和数字; 智力障碍; }; //使用随机生成的特定数量的整数创建填充数据文件

我想得到一些关于以下代码行的帮助。 对于constructArray函数,我无法按如下所示运行它!一个文件作为真实的信息,但我不知道是什么错误。 非常感谢你的帮助 还有如何使用.txt创建infle文件名,我已经尝试使用+“.txt”缩进,但由于参数中的文件类型,我无法这样做。 编译器运行映像:

使用名称空间std;
枚举NumType{奇数,偶数};
结构编号
{
国际贸易编号;
核型型;
整数位数;
整数位数;
整数和数字;
智力障碍;
};
//使用随机生成的特定数量的整数创建填充数据文件
void-constructInfile(fstream&aFile,字符文件名[]);
//从infle txt文件读取数据并传输到数字数组
int constructArray(fstream&aFile,const char fileName[],Number run[]);
/*
void processArray(数字[],int);
//从数组传输信息,并以特定的信息格式存储到名为outfile txt的输出文件中
无效数组输出文件(fstream&,char[],Number[],int);
*/
常数int MAX=50;
int main()
{
srand(时间(空));
流文件;
字符文件名[MAX];
cout文件名;
constructInfile(文件名);
运行次数[MAX];
int recNo=constructArray(文件名,ran);

您可能已打开文件,但忘记关闭它。在两个函数结束时,请关闭
文件
。这应该可以解决问题。

您在ios::out模式下打开了一个文件,但从未关闭过它

您再次尝试在ios::in模式下打开同一个文件。如何在不正确关闭fstream的情况下在两种不同模式下打开一个文件两次


您需要关闭函数
constructInfile()中的文件流
!!!

如果解决方案无法解决问题,请检查文件是否已创建,并在此处发布错误消息。只有第二个函数无法工作。我已尝试添加cerr@MATU yes。第二个函数无法打开文件,因为它已被第一个函数打开。因此需要在第一个函数结束时关闭它一般规则是在操作后关闭文件。这就是为什么您还需要在第二个函数中关闭文件(最佳实践)。
    using namespace std;

    enum NumType {Odd, Even};

    struct Number
    {
         int no;
         NumType type;
         int oddDigits;
         int evenDigits;
         int sumDigits;
         int noDigits;
    };

    // Create inFile data file with certain number of integers which are randomly generated
    void constructInfile (fstream& aFile, char fileName[]);


    // Read data from infile txt file and transfer to array of numbers
    int constructArray (fstream& aFile,const char fileName[], Number ran[]);

    /*
    void processArray (Number [ ], int);

    // Transfer information from array and store into output file called outfile txt with specific information format
    void arrayToOutfile (fstream&, char [ ], Number [ ], int);
    */


    const int MAX = 50;

    int main()
    {
    srand(time(NULL));
    fstream aFile;
    char fileName [MAX];

    cout << "Enter designated file name to be created" << endl;
    cin >> fileName;

    constructInfile (aFile,fileName);

    Number ran[MAX];
    int recNo = constructArray(aFile,fileName,ran);
    cout << recNo << " of records transferred" << endl;

    }

    // Create inFile data file with certain number of integers which are randomly generated
    void constructInfile (fstream& aFile,char fileName[]){
        aFile.open(fileName, ios::out);

        if(aFile.fail()){
            cout << "File open unsuccessful" << endl;
            aFile.close();
            exit(1);
        }

        cout << "Begin creation of " << fileName << " file" << endl << endl;

        int size = rand()%51+50;

        for(int a = 0;a < size;a++){
            aFile << rand()%1000+1 << endl;
        }

        cout << fileName << " file successfully created" << endl;

    }


    // Read data from infile txt file and transfer to array of numbers
    int constructArray (fstream& aFile,const char fileName[], Number ran[]){

    aFile.open (fileName, ios::in);

        if (!aFile)
        {
            cout << fileName << " failed to open" << endl;
            aFile.close ();

            return 0;
        }

        cout << "Begin from " << fileName << " to array" << endl;

        int i = 0;

        char tabKey;

        while (aFile >> ran[i].no)
        {
            aFile.get (tabKey);     // read and discard
            i++;
        }

        aFile.close ();
        cout << fileName << " to array done" << endl;

        return i;
    }