2016-12-11 3 views
1

Im работает с этим кодом, но мой компилятор не хочет запускать его и давать мне ошибку в строке 47 infile.open (fileloc, ios :: in);ошибка с объектом fstream

любые предложения? Thank you

#include<iostream> 
#include<iomanip> 
#include<sstream> 
#include<fstream> 

using namespace std; 

void encrypt(); // encrypt function protype 
void decrypt(); 

int main() 
{ 
    int choice; 
    bool done = false; 

    while(!done){ 
     cout << "What you want to do?\n1) Encrypt a file.\n2) Decrypt a file." << endl; 
     cin >> choice; 

     if (choice == '1') 
     { 
      encrypt(); 
      done = true; 
     } 
     if (choice == '2') 
     { decrypt(); 
      done = true; 
     } 
     else 
     { cout <<"Invalid input try again"<<endl; 
     } 
    } 

} 

void encrypt() 
{ 
    string outfile; 
    string fileloc; 
    string x; 
    char y; 
    fstream infile; 
    fstream outputFile; 

    cout << "What is the name of the file you want to encrypt? " << endl; 
    cin >> fileloc; 
    infile.open(fileloc,ios::in); 

    while(!infile){ 
    cout << "Your file does not exist, enter a valid file name..."<< endl; 
    cin >> fileloc; 
    infile.open(fileloc,ios::in); 
    } 
    cout << "What is the name of your output file?" << endl; 
    cin >> outfile; 
    outputFile.open(outfile,ios::out); 
    while(infile>>noskipws>>y){ 
     outputFile << char(y+10); 
    } 
} 

void decrypt() 
{ 
     string outfile; 
     string fileloc; 
     char y; 
     fstream infile; 
     fstream outputFile; 

     cout << "What is the name of the file you want to decrypt? " << endl; 
     cin >> fileloc; 
     infile.open(fileloc,ios::in); 

     while(!infile){ 
     cout << "Your file does not exist, enter a valid file name..."<< endl; 
     cin >> fileloc; 
     infile.open(fileloc,ios::in); 
     } 
     cout << "What is the name of your output file?" << endl; 
     cin >> outfile; 
     outputFile.open(outfile,ios::out); 
     while(infile>>noskipws>>y){ 
       outputFile << char(y-10); 
    } 
} 

Эта программа должна выполнить базовое шифрование, добавив 10 к каждому символу ASCII в файле.

+0

Вы пробовали ios_base :: в где у вас есть ios :: in? –

+0

или std :: ios :: in –

+0

Возможный дубликат http://stackoverflow.com/questions/13321644/c-using-fstream –

ответ

0

В infile.open(fileloc, ios::in)

Это не работает во всех стандартах. Если вы используете C++ 11, все в порядке. Однако многие компиляторы используют старые стандарты. Вы можете попробовать использовать

infile.open(fileloc.c_str(), ios::in)

Это получает с-строковое представление ваших std::string, который совместим с fstream::open.

Если вы посмотрите here, вы увидите, что C++ используется только для разрешения const char * в качестве аргумента для пути.

Вы также можете убедиться, что используете совместимый стандарт, который поддерживает const std::string& как тип аргумента для пути.

Смежные вопросы