正在读取文件C++Ifstream

Reading from a file C++ Ifstream

本文关键字:C++Ifstream 文件 读取      更新时间:2023-10-16

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

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

我认为更好的方法是使用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<<"errorn";
      continue;
    }
    int value;
    bool isint = istringstream(elems[1])>>value;
    if(!isint)
    {
      cout << "errorn";
      continue;
    }
    cout<<"value was : " << value << endl;
  }
  return 0;
}

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