Having trouble validating a file over and over again

时间:2016-07-11 22:02:41

标签: c++ file validation

One of the things my program needs to do is validate a file using the isValid function entered by user and it will keep doing this until exit is entered and if I enter nothing but valid file names there are no problems. But when I enter an invalid file name followed by a valid file name it still says the file is invalid and I cannot figure out why and I have tried debugging it and what not and still cannot find the problem. Any help would be greatly appreciated!

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

    }
}

2 个答案:

答案 0 :(得分:1)

ifstream my_file(file_name.c_str());

This doesn't replace the my_file you'd already created in an outer scope. It just makes a new local variable that lives for like a nanosecond.

You'll have to close then re-open the existing my_file, being sure to reset its error flags too.

答案 1 :(得分:0)

The logic you are using to exit the loop is flawed.

You need to check the value of file_name right after it is entered, not after it is processed in the while loop once.

You need to use something along the lines of:

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

where

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

Other improvements:

  1. The call to cin.ignore() is going to be a problem line since std::getline does not leave the newline character in the input stream. You'll have to type Enter one more time. You should remove it.

  2. You don't need the cin.clear() line. You need cin.clear() only if an error was detected in reading from the stream -- such as when using cin >> var; when the input stream did not have the right data suitable for var.

  3. You don't need to open the file if the file is not valid.

  4. You don't need multiple lines ifstream my_file(file_name.c_str());. You only need it once, just before the call to Output_function(my_file).

  5. You don't need to explicitly call my_file.close(). The file will be closed and the end of the scope.

Here's a simplified version of 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);
   }
}