2016-04-05 18 views
0

Buna benzer başka konular var, ancak hiçbiri tamamen aynı sorunla ve hiçbiri benim için çalışmıyor. İşte Ben ile çalışıyorum açılış kodu:ifstream dosyası açılamıyor

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

dosya MemberInfo.txt (Ben kuyruk ucunu COLUMN_NAME ve doğrudan yolu, MemberInfo, MemberInfo.txt ve MemberInfo.txt.txt çalıştım

Geçerli dizindir ve ne yaptığım önemli değil, hala açılmıyor.

Herhangi bir öneriniz var mı? Düzenleme

: Eğer AC tarzı dize için dosya dönüştürmek durumunda İşte (. Öğeler dahil muhtemelen gerekmez)

#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; 
} 
+0

C++ 11, ['open' yönteminde] 'std :: string' kullanımı için destek ekler (http://en.cppreference.com/w/cpp/io/basic_ifstream/open). Derleyiciniz bir ton yeni özellikte eksik olduğunuzu desteklemiyorsa, mümkünse yükseltmeye değer. – tadman

+0

Bu proje için Visual Studio 2012 kullanıyorum. – lynnrhyann

+0

İdeal olarak 2015'e geçebilir ve sadece C++ 11'deki gelişmelerden değil C++ 14'den de yararlanabilirsiniz. Orada bir sürü yeni şey var (https://msdn.microsoft.com/en-us/library/hh567368.aspx?f=255&MSPPError=-2147217396). – tadman

cevap

-1

bugüne kadar tam kodu fstream.open() çalışması gerekir o. Dönüşüm için .c_str() öğesini kullanabilirsiniz. En azından bu, dizeleri c stil dizelerine dönüştürdüğümde yaptığım şey. İşte

inputFile.open(filename.c_str()); 

Kodunuzdaki iki konu var ifstream's open function

+0

Ayrıca, c string değiştiricisini de değişken bildirimime eklemem gerekir mi? – lynnrhyann

+0

Hayır, sadece açık fonksiyondaki dönüşüm bunu yapmalıdır. – Amir

+0

Sadece mod ve hiçbir zar ile çalıştırmayı denedim. Başka önerileriniz var mı? – lynnrhyann

0

üzerinde dokümandır. Bir süre döngüsünde

  1. Eğer inputFile.open("filename"); yerine inputFile.open(filename); (tırnak) bulunmaktadır. Bu süre döngü muhtemelen if(inputFile) satırından önce olmalıdır. Aksi halde yanlış dosya adını verdiğinizde geri dönme şansınız yoktur.

(Ben de while (!inputFile) için while (inputFile == false) değiştirmek istiyorum.) Istendiğinde

Üstelik, bir kullanıcı programı çalıştırır hangi dizine dosya göreli yolunu vermelidir. Aksi halde, bulamazdı.

+0

Tamam, bu hataları düzeltin (teşekkürler) ve hatta doğrudan bir yolla dosyayı bulamazsınız – lynnrhyann

+0

Hmm. Kodunuzu derledim ve çalışıyor. Yeni bir dosya oluşturmayı denediniz mi, hatta boş mu? Bir dosya korumalı olarak okunursa, onu da bulamaz. Ayrıca, programı IDE'den çalıştırıyorsanız, bir konsoldan çalıştırmayı deneyin. – ptrj