【问题标题】:Reading from a file C++ Ifstream从文件中读取 C++ Ifstream
【发布时间】:2015-12-08 03:57:13
【问题描述】:

我正在尝试使用动态编程来解决问题,以在最大化价值的同时找到在一定重量下从文本文件中选择哪些项目。文件格式为:

item1, weight, value
item2, weight, value

具有可变数量的项目。我在 main 方法中读取文件时遇到问题,我正在尝试对错误输入进行错误检查。我的问题来自于我检查权重是否丢失,或者是一个字符串而不是一个 int。我不知道如何单独检查字符串,或者该位置是否没有任何内容。我应该根据错误输出不同的错误。谢谢。

int main(int argc, char * const argv[])
{
    std::vector<Item> items;
    if (argc != 3)
    {
        std::cerr << "Usage: ./knapsack <capacity> <filename>";
        return -1;
    }

    int capacity;
    std::stringstream iss(argv[1]);

    if (!(iss >> capacity) || (capacity < 0))
    {
        std::cerr << "Error: Bad value '" << argv[1] << "' for capacity." << std::endl;
        return -1;
    }   

    std::ifstream ifs(argv[2]);
    if (ifs.is_open())
    {
        std::string description;
        unsigned int weight;
        unsigned int value;
        int line = 1;
        while (!ifs.eof())
        {
            if (!(ifs >> description))
            {
                std::cerr << "Error: Line number " << line << " does not contain 3 fields." << std::endl;
                return -1;
            }

            if (!(ifs >> weight))
            {
                if (ifs.eof())
                    std::cerr << "Error: Line number " << line << " does not contain 3 fields." << std::endl;
                else
                    std::cerr << "Error: Invalid weight '" << ifs << "' on line number " << line << "." << std::endl;
                return -1;
            }

            else if (!(ifs >> weight) || (weight < 0))
            {
                std::cerr << "Error: Invalid weight '" << ifs << "' on line number " << line << "." << std::endl;
                return -1;
            }

            if (!(ifs >> value))
            {
                if (ifs.eof())
                    std::cerr << "Error: Line number " << line << " does not contain 3 fields." << std::endl;
                else
                    std::cerr << "Error: Invalid value '" << ifs << "' on line number " << line << "." << std::endl;
                return -1;
            }

            else if (!(ifs >> value) || (value < 0))
            {
                std::cerr << "Error: Invalid value '" << ifs << "' on line number " << line << "." << std::endl;
                return -1;
            }

            Item item = Item(line, weight, value, description);
            items.push_back(item);
            line++;
        }

        ifs.close();
        knapsack(capacity, items, items.size());
    }

    else
    {
        std::cerr << "Error: Cannot open file '" << argv[2] << "'." << std::endl;
        return -1;
    }

    return 0;
}

【问题讨论】:

    标签: c++ file-io ifstream


    【解决方案1】:

    我认为更好的方法是使用getline 函数,通过该函数您将取一整行,然后您可以尝试将其拆分为三个字符串。如果没有找到三个部分或三个部分中的任何一个格式不正确,则可以输出错误。

    下面给出一个示例代码,

    #include<fstream>
    #include<iostream>
    #include<vector>
    #include <sstream>
    #include<string>
    
    using namespace std;
    
    void split(const std::string &s, std::vector<std::string> &elems, char delim) {
      elems.clear();
      std::stringstream ss(s);
      std::string item;
      while (std::getline(ss, item, delim)) {
        elems.push_back(item);
      }
    }
    
    int main()
    {
      vector<string> elems;
      ifstream fi("YOUR\\PATH");
    
      string inp;
      while(getline(fi, inp))
      {
        split(inp, elems, ' ');
    
        if(elems.size() != 3)
        {
          cout<<"error\n";
          continue;
        }
    
        int value;
        bool isint = istringstream(elems[1])>>value;
        if(!isint)
        {
          cout << "error\n";
          continue;
        }
    
        cout<<"value was : " << value << endl;
      }
      return 0;
    }
    

    【讨论】:

    • 您使用.eof() 作为循环终止符是错误的。请改用std::getline(fi, inp);。 (将std::string inp; 提升到循环之外。)
    • 感谢@Md.SumsuddinShojib 的这篇文章,但是我仍然有问题,因为分隔符是“,”,而不仅仅是“”字符。
    【解决方案2】:

    您可以一次阅读整行并使用正则表达式检查其格式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-04
      • 2011-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多