c++ ifstream无法打开文件

r3i60tvu  于 2023-01-10  发布在  其他
关注(0)|答案(3)|浏览(397)

还有其他类似的主题,但没有一个是完全相同的问题,没有一个解决方案是为我工作。以下是我正在使用的开放代码:

ifstream inputFile;
 string filename;

 cout << " Tablerock Member Services" << endl;
 cout << "***************************\n\n" << endl;
 cout << " Please enter the name of the member file: ";
 cin >> filename;

 inputFile.open(filename);

文件名为MemberInfo.txt(我取消了尾部隐藏,并尝试了直接路径,MemberInfo、MemberInfo.txt和MemberInfo.txt.txt)
它是当前目录,无论我怎么尝试,它仍然打不开。
有什么建议吗?
编辑:以下是到目前为止的完整代码(包括您可能不需要的项目)。

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int FindInfo();

int main()
{
 //declare
 int choice = 0;
 char ans = '\0';
 auto stuff = 0;
 const int SEARCH_CHOICE = 1;
 const int ADD_CHOICE = 2;
 const int EDIT_CHOICE = 3;
 const int DISPLAY_CHOICE = 4;
 const int QUIT_CHOICE = 5;

 ifstream inputFile;
 string filename;

 cout << " Tablerock Member Services" << endl;
 cout << "***************************\n\n" << endl;
 cout << " Please enter the name of the member file: ";
 cin >> filename;

 inputFile.open(filename.c_str());

 if(inputFile)
 {
    do
     {

     cout << "\n";
     cout << " 1. Find a member's information" << endl;
     cout << " 2. Add a member to the database" << endl;
     cout << " 3. Edit a member's information" << endl;
     cout << " 4. Display all records" << endl;
     cout << " 5. Quit" << endl;

     cout << " Please choose an option from the menu:";    
     cin >> choice;

     switch(choice)
     {
        case SEARCH_CHOICE:
        FindInfo();
        cout << " Would you like to choose again?";
        cin >> ans;
        break;

        case ADD_CHOICE:
        cout << " You chose option 2." << endl;
        cout << " Would you like to choose again?";
        cin >> ans;
        break;

        case EDIT_CHOICE:
        cout << " You chose option 3." << endl;
        cout << " Would you like to choose again?";
        cin >> ans;
        break;

        case DISPLAY_CHOICE:
        cout << " You chose option 4." << endl;
        cout << " Would you like to choose again?";
        cin >> ans;
        break; 

        case QUIT_CHOICE:
        cout << " Press Q to exit the program or y/Y to select another option.";
        cin >> ans;
        break;
         }//end switch
     }while( ans == 'y' || ans == 'Y');
 }

 else while (inputFile == false)
 {
     cout << "There was an error opening the file. \n Please enter the filename: ";
     cin >> filename;
     inputFile.open("filename");
 }

 return 0;
}

int FindInfo()
{
    const int SIZE = 21;
    char lname[SIZE];
    char ans = '0';

    cout << "\n\n Search for a member:" << endl;
    cout << "\n Please enter the last name of the member you are trying to find." << endl;
    cout << " Last Name: ";
    cin >> lname;    
    cout << " You entered " << lname << "." << endl;
    cout << " Is this correct? (y/n)";
    cin >> ans;

    if( ans == 'y' || ans == 'Y')
    {
        cout << " Great! Please wait while we find the member in our database..." << endl;
    }

    else while( ans == 'n' || ans == 'N')
    {
        cout << " Please enter the last name of the member you are trying to find." << endl;
        cout << " Last Name: ";
        cin >> lname;    
        cout << " You entered " << lname << "." << endl;
        cout << " Is this correct? (y/n)";
        cin >> ans;

        if( ans == 'y' || ans == 'Y')
        {
            cout << " Great! Please wait while we find the member in our database..." << endl;
        }

    }


    return 0;
}
rdrgkggo

rdrgkggo1#

您的代码中存在两个问题。
1.在while循环中,使用inputFile.open("filename");代替inputFile.open(filename);(引号)。
1.这个while循环应该在if(inputFile)行之前,否则当你给予错误的文件名时就没有机会返回了。
(我也会将while (inputFile == false)更改为while (!inputFile)。)
此外,当系统提示时,用户应该给予文件相对于他执行程序的目录的路径,否则,它将找不到它。

euoag5mw

euoag5mw2#

我刚到这里是因为我遇到了同样的问题。我读了答案,他们似乎遗漏了一个可能导致open方法失败的原因,因为OP的代码完全没有问题。
我认为,问题在于OP代码试图打开的文件已经在IDE或任何其他地方打开,这会阻塞open函数,就好像它被调用了两次一样。
这一点可能不太明显,所以我决定在这里为后代降一行:)

vfhzx4xs

vfhzx4xs3#

如果你把你的文件名转换成一个c风格的字符串,fstream.open()应该可以处理它,你可以使用.c_str()来转换,至少这是我把字符串转换成c风格字符串时所做的。

inputFile.open(filename.c_str());

以下是有关ifstream's open function的文档

相关问题