ifstream无法打开文件

时间:2016-04-05 23:12:54

标签: c++ ifstream

还有其他与此相似的主题,但没有一个具有完全相同的问题,并且这些决议都不适用于我。 这是我正在使用的开场代码:

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;
}

2 个答案:

答案 0 :(得分:0)

您的代码中存在两个问题。

  1. 在while循环中,您有inputFile.open("filename");而不是inputFile.open(filename);(引号)。

  2. 这个while循环应该在行if(inputFile)之前。否则,当您提供错误的文件名时,将无法返回。

  3. (我也将while (inputFile == false)更改为while (!inputFile)。)

    此外,当提示时,用户应该提供相对于他执行程序的目录的文件路径。否则,它就找不到它。

答案 1 :(得分:-1)

如果将文件名转换为c样式字符串,fstream.open()应该可以使用它。您可以使用.c_str()进行转换。至少这是我将字符串转换为c样式字符串时所做的。

inputFile.open(filename.c_str());

以下是ifstream's open function

的文档