【问题标题】:Having trouble validating a file over and over again一遍又一遍地验证文件时遇到问题
【发布时间】:2016-07-11 22:02:40
【问题描述】:

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

    }
}

【问题讨论】:

    标签: c++ file validation


    【解决方案1】:
    ifstream my_file(file_name.c_str());
    

    这不会替换您已经在外部作用域中创建的my_file。它只是创建了一个新的局部变量,它的生命周期只有一纳秒。

    您必须关闭然后重新打开现有的my_file,并确保也重置其错误标志。

    【讨论】:

      【解决方案2】:

      你用来退出循环的逻辑有缺陷。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-04-16
        • 1970-01-01
        • 2017-10-10
        • 2023-03-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多