2012-04-10 13 views
0
#include <iostream> 
    #include <fstream> 
    #include <string> 
    using namespace std; 

    // Main Routine 

    void main() { 
     char in; 
     string s,m; 
     fstream f; 

    // Open file 
     cout << "Positive Filter Program\n"<< endl; 
     cout << "Input file name: "; 
     cin >> s; 
     cout << "Output file name: "; 
     cin >> m; 
     f.open(s.data(),ios::in); 
     f.open(m.data(),ios::out); 

    // Loop through file 

    if(f.is_open()) 
    { 
     while(f.good()) 
     { 
      f.get(in); 
      f<<in; 
      cout << "\nFinished!"<< endl; 
     } 
    } 
    else cout << "Could not open file"; 

    // Close file 
    f.close(); 

    } 

ここで間違っているのはわかりません。このプログラムでは、私はあなたが入力した内容のファイル名にして出力を考え、入力するファイル名をCINしようとしている、としていますファイルの入出力はC++で

+0

あなたは、このコードで直面している問題は何ですか? – Naveen

+1

'fstream'オブジェクトを1つ使用していますか?入力用にもう1つ*出力用にもう1つ*したくありませんでしたか? – trojanfoe

答えて

3

を同じfstreamオブジェクトが再利用されている:。

f.open(s.data(),ios::in); 
f.open(m.data(),ios::out); 

それは決して入力ファイルを読み込みます。 whileループが間違っている

std::ifstream in(s.data()); 
std::ofstream out(m.data()); 

、読み取りの試みの結果は、読み取り後すぐにチェックする必要があります:に変更し

char ch; 
while(in.get(ch)) 
{ 
    out << ch; 
} 
cout << "\nFinished!"<< endl; // Moved this to outside the while 
+0

ありがとう、それは助け! – MIkey27

関連する問題