2017-09-13 26 views
1

私のコードをコンパイルすることはできません。なぜなら、エラー16行目に「エラー:呼び出しに一致する関数がありません。私はファイルを読んで、すべての母音を出力ファイルに書き込むと仮定しています。これにC++の入力ファイルと出力ファイルへの書き込み

inputfile.open(filename); 

:この

#include <iostream> 
#include <fstream> 
#include <string> 
using namespace std; 

int main(){ 
    string filename; // to hold the file name 
    ifstream inputfile; // input to a file 


    // Get the file name 
    cout << "Enter a file name: "; 
    cin >> filename; 

    // Open the file 
    inputfile.open(filename); // LINE 16 

    char vowel; // to store the vowels 
    ofstream outputfile; // to write to the file 

    // open file 
    outputfile.open("vowels_.txt"); 

    while(inputfile.get(vowel)){ 
     //If the char is a vowel or newline, write to output file. 
     if((vowel == 'a')||(vowel == 'A')||(vowel =='e')||(vowel =='E')||(vowel =='i')||(vowel =='I')||(vowel =='o')||(vowel =='O')||(vowel =='u')||(vowel =='U')||(vowel =='\n') && !inputfile.eof()) 
      outputfile.put(vowel); 

    } 

    inputfile.close(); 
    outputfile.close(); 



} 
+0

ライン17空白行です。 – immibis

+0

@immibis line 16、申し訳ありません。 –

+0

私はそれがopen()コールだと思います。 'inputfile.open(filename.c_str());' – Rene

答えて

2

変更

inputfile.open(filename.c_str()); 

filename以降はstd::stringで、fstream::openをパラメータとしてconst char* filenameを取ります。

string:c_strを呼び出すと、const char*std::stringから返されます。


C++ 11 fstream::openも同様std::stringを取るために、オーバーロードされているので、これを必要としません。 -std=c++11フラグを指定してコンパイルすると、C++ 11が有効になります。


PS:Why don't the std::fstream classes take a std::string?は(プレC++ 1)

+0

OH OKAY。ありがとうございます –

+0

どうすればいいですか? –

+0

* std :: string'を使用することはできません。 –

関連する問題