Es gibt andere ähnliche Themen, aber keine mit dem gleichen Problem, und keine dieser Lösungen funktioniert für mich. Hier ist der Code Öffnung mit denen ich arbeite:ifstream kann die Datei nicht öffnen
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);
Der Dateiname ist MemberInfo.txt (I unhid das hintere Ende und ich habe einen direkten Weg versuchen, Member, MemberInfo.txt und MemberInfo.txt.txt)
Es ist das aktuelle Verzeichnis, und egal was ich versuche, es wird immer noch nicht geöffnet.
Irgendwelche Vorschläge?
Edit: Hier wird der vollständige Code ist so weit (. Einschließlich Elemente, die Sie wahrscheinlich nicht brauchen)
#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;
}
C++ 11 fügt Unterstützung für die Verwendung von 'std :: string' in der [' open' Methode] (http://en.cppreference.com/w/cpp/io/basic_ifstream/open) hinzu. Wenn Ihr Compiler nicht unterstützt, dass Sie eine Menge neuer Funktionen verpassen, lohnt es sich, wenn möglich zu aktualisieren. – tadman
Ich verwende Visual Studio 2012 für dieses Projekt. – lynnrhyann
Im Idealfall können Sie auf 2015 umsteigen und nicht nur die Verbesserungen in C++ 11, sondern auch in C++ 14 nutzen. Es gibt [viele neue Sachen dort] (https://msdn.microsoft.com/en-us/library/hh567368.aspx?f=255&MSPPError=-2147217396), die einen Besuch wert sind. – tadman