2016-07-11 6 views
0

私のプログラムは、ユーザーが入力したisValid関数を使ってファイルを検証する必要があります。終了するまでこれを続けます。名前は問題ありません。しかし、無効なファイル名の後に有効なファイル名を入力すると、それでもファイルが無効であると言われます。理由を理解できず、デバッグしようとしましたが、何も問題が見つかりません。どんな助けでも大歓迎です!何度も何度もファイルを検証するのに問題がある

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

void Open_file(string name) 
{ 
    ifstream my_file; 
    my_file.open(name.c_str()); 
} 


bool isValid(ifstream& file, string name) 
{ 
    if ((name.substr(name.length() - 4)) != (".htm")) 
    { 
     return false; 
    } 

    cout << file << endl; 
    if (file.good()) 
    { 
     return true; 
    } 

    else 
    { 
     return false; 
    } 
} 


string File_title(ifstream& my_file) 
{ 
    string title; 
    string line; 
    size_t first_title; 
    size_t second_title; 
    string str; 

    while((getline(my_file,line))) 
    { 
     str = str + line; 
    } 

    first_title = str.find("<title>"); 
    second_title = str.find("</title>"); 
    title = str.substr(first_title + 7, (second_title) - (first_title + 7)); 

    return title; 
} 


void Output_function(ifstream& my_file) 
{ 

    string line; 
    ifstream MyFile("titles.txt"); 


    string g = File_title(my_file); 
    while(getline(MyFile, line)) 
    { 
     if((g == line)) 
     { 
      return; 
     } 
    } 

    ofstream out_title("titles.txt", fstream::app); 
    out_title << g << endl ; 
} 

void Clear_file() 
{ 

    ofstream out_title("titles.txt"); 
    out_title << "" << endl; 

} 



int main() 
{ 

    string file_name; 

    while (file_name != "exit") 
    { 
     cout <<"please enter a HTML file name or hit 'exit' to quit and " << endl; 
     cout << "if you want to clear file please enter 'clear': "; 
     getline(cin,file_name); 
     ifstream my_file(file_name.c_str()); 
     cin.ignore(256, '\n'); 
     if(file_name == "clear") 
     { 
      Clear_file(); 
      break; 

     } 
     while ((isValid(my_file, file_name) == false)) 
     { 
      cin.clear(); 
      cout <<"Invalid file name, please enter a valid file name: "; 
      getline(cin,file_name); 
      ifstream my_file(file_name.c_str()); 


     } 

     Open_file(file_name); 
     Output_function(my_file); 



     my_file.close(); 

    } 
} 

答えて

1
ifstream my_file(file_name.c_str()); 

これは、あなたがすでに外側のスコープで作成したいmy_fileに代わるものではありません。それはちょうどナノ秒のように生きる新しいローカル変数を作るだけです。

既存のmy_fileをもう一度開いて、エラーフラグもリセットしてください。

0

ループを終了するために使用しているロジックに欠陥があります。

whileループで処理された後ではなく、入力後すぐにfile_nameの値を確認する必要があります。

あなたはの線に沿って何かを使用する必要があります。

while ((file_name = get_file_name()) != "exit") 
{ 
    ... 
} 

std::string get_file_name() 
{ 
    std::string file_name; 
    cout <<"please enter a HTML file name or hit 'exit' to quit and " << endl; 
    cout << "if you want to clear file please enter 'clear': "; 
    getline(cin,file_name); 
    return file_name; 
} 

その他の改良点:

  1. cin.ignore()への呼び出しがあることを行っています012からの問題の行は入力ストリームに改行文字を残しません。 と入力する必要があります。もう一度と入力してください。削除する必要があります。

  2. cin.clear()行は必要ありません。入力ストリームがvarに適した適切なデータを持たない場合にcin >> var;を使用する場合など、ストリームからの読み取りでエラーが検出された場合のみ、cin.clear()が必要です。

  3. ファイルが有効でない場合は、ファイルを開く必要はありません。

  4. 複数行は必要ありませんifstream my_file(file_name.c_str());。あなたはOutput_function(my_file)への呼び出しの直前に一度だけ必要です。

  5. my_file.close()を明示的に呼び出す必要はありません。ファイルは閉じられ、スコープの終わりです。

mainの簡略化したバージョンです。

int main() 
{ 
    string file_name; 

    while ((file_name = get_file_name()) != "exit") 
    { 
     if(file_name == "clear") 
     { 
     Clear_file(); 
     break; 
     } 

     while (isValid(my_file, file_name) == false) 
     { 
     cout <<"Invalid file name, please enter a valid file name: "; 
     getline(cin,file_name); 
     } 

     Open_file(file_name); 
     ifstream my_file(file_name.c_str()); 
     Output_function(my_file); 
    } 
} 
関連する問題