正在读取文件C++Ifstream
Reading from a file C++ Ifstream
我正在尝试使用动态编程来解决一个问题,以在一定的权重容量下从文本文件中选择哪些项目,同时最大化价值。该文件的格式为:
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;
}
您可以一次读取整行,并使用正则表达式检查其格式。
相关文章:
- ifstream文件在从行中读取时被清除
- 是否可以在没有真实文件的情况下创建 ifstream
- 虽然循环不读取整个 ifstream 文件
- std::ifstream 在读取文件中最后一项时设置 eofbit,但仅在读取数值类型时发生
- C++:std::ofstream 方法 open() 在第二次迭代时擦除打开的 ifstream 文件
- ifstream 读取的字符与文件中写入的字符不同
- 如何使用 ifstream 检查文件中变量的类型?
- 尝试使用 Ifstream 打开.txt文件时C++问题
- 如何确保我的"ifstream"文件对象指向的文件内容已更新?
- 为什么我可以将 ifstream 文件置于 if 条件?
- 将 ifstream 文件和每一行读取到数组中
- 为什么 ifstream 文件中的换行符 - 当被此代码读取时 - 占用 2 个字节
- 带有引号+组合目录 c++ 的 ifstream 文件目录
- 函数不读取 ifstream 文件
- 尝试将数字放入 ifstream 文件中的变量中
- Ifstream文件不会打开,尽管一切似乎都已就绪(c++)
- ifstream 文件打开导致程序'terminate in an unusual way'
- 从ifstream文件读取时给出了错误的大小
- 通过函数传递ifstream文件时出现错误
- 在fstream/ifstream文件中使用多个函数