一遍又一遍地验证文件时遇到问题

Having trouble validating a file over and over again

本文关键字:一遍 遇到 问题 验证 文件      更新时间:2023-10-16

我的程序需要做的一件事是使用用户输入的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();
    }
}
ifstream my_file(file_name.c_str());

这不会替换已在外部作用域中创建的my_file。它只是创造了一个新的局部变量,其寿命像纳秒一样。

您必须关闭然后重新打开现有my_file,请务必重置其错误标志。

用于退出循环的逻辑存在缺陷。

您需要在输入 file_name 的值后立即检查它,而不是在 while 循环中处理一次后。

您需要使用类似于以下内容的内容:

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()的调用将是一个问题行,因为std::getline不会在输入流中留下换行符。您必须再键入一次 Enter 键。您应该将其删除。

  2. 您不需要cin.clear()行。仅当从流中读取时检测到错误时,才需要cin.clear() - 例如,当输入流没有适合var的正确数据时,使用 cin >> var; 时。

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