基于C++文本文件的数据库读取 我需要C++中的简单程序,它可以打开文本文件,逐行查找第二个参数。

基于C++文本文件的数据库读取 我需要C++中的简单程序,它可以打开文本文件,逐行查找第二个参数。,c++,arrays,text-files,C++,Arrays,Text Files,文本文件的内容: 1, red 2, blue 3, green 4, orange 所以我需要一个程序,它将一行接一行,每一行变成两个元素的数组,然后将第一个元素和用户交互插入的数字进行比较 所以,如果用户插入2,它将逐行进行,比较数组的第一个元素,如果匹配,它将以蓝色打印数组中的第二个元素,如果用户输入3,它将以绿色打印 我一直在用PHP工作,这比这容易多了,所以我现在有点受不了了…:/ 如何在C++中打开文本文件? 问题2:如何逐行读取文件 问题3:如何将带分隔符的字符串转换为基于该分隔

文本文件的内容:

1, red
2, blue
3, green
4, orange
所以我需要一个程序,它将一行接一行,每一行变成两个元素的数组,然后将第一个元素和用户交互插入的数字进行比较

所以,如果用户插入2,它将逐行进行,比较数组的第一个元素,如果匹配,它将以蓝色打印数组中的第二个元素,如果用户输入3,它将以绿色打印

我一直在用PHP工作,这比这容易多了,所以我现在有点受不了了…:/

如何在C++中打开文本文件? 问题2:如何逐行读取文件

问题3:如何将带分隔符的字符串转换为基于该分隔符的数组

如何在C++中打开文本文件? 问题2:如何逐行读取文件

问题3:如何将带分隔符的字符串转换为基于该分隔符的数组

给你:

#include <iostream>
#include <fstream>
#include <map>
#include <sstream>

int main(int argc, char* argv[]) {
  if (argc != 2) {
    std::cerr << "Usage: test [inputfile]" << std::endl;
    return 1;
  }
  std::fstream stream(argv[1]);
  if (!stream.good()) {
    std::cerr << "Error: could not open file: " << argv[1] << std::endl;
    return 2;
  }
  std::string line;
  std::map<int, std::string> map;
  while (std::getline(stream, line)) {
    std::string::size_type pos = line.find(',');
    std::stringstream sstream(line.substr(0, pos));
    int index;
    sstream >> index;
    map[index] = line.substr(pos+2);
  }
  int in;
  while (std::cin >> in) {
    std::map<int, std::string>::iterator i = map.find(in);
    if (i == map.end())
      std::cerr << "index not found" << std::endl;
    else
      std::cout << i->second << std::endl;
  }
  return 0;
}
给你:

#include <iostream>
#include <fstream>
#include <map>
#include <sstream>

int main(int argc, char* argv[]) {
  if (argc != 2) {
    std::cerr << "Usage: test [inputfile]" << std::endl;
    return 1;
  }
  std::fstream stream(argv[1]);
  if (!stream.good()) {
    std::cerr << "Error: could not open file: " << argv[1] << std::endl;
    return 2;
  }
  std::string line;
  std::map<int, std::string> map;
  while (std::getline(stream, line)) {
    std::string::size_type pos = line.find(',');
    std::stringstream sstream(line.substr(0, pos));
    int index;
    sstream >> index;
    map[index] = line.substr(pos+2);
  }
  int in;
  while (std::cin >> in) {
    std::map<int, std::string>::iterator i = map.find(in);
    if (i == map.end())
      std::cerr << "index not found" << std::endl;
    else
      std::cout << i->second << std::endl;
  }
  return 0;
}

这可能没有什么帮助,除了我;我开始练习使用精神解析和业力输出生成作为奖励:

#include <boost/spirit/include/karma.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/include/std_pair.hpp>
#include <fstream>
#include <map>

namespace qi = ::boost::spirit::qi;
namespace karma = ::boost::spirit::karma;

typedef std::map<int, std::string> index_t;

index_t read_index(const char* filename)
{
    using boost::spirit::istream_iterator;
    using namespace qi;

    index_t result;
    std::ifstream ifs(filename);
    ifs.unsetf(std::ios::skipws);
    istream_iterator begin(ifs), end;

    if (!parse(begin, end, (int_ >> omit[lit(',') >> *char_(" \t")] >> *(char_ - eol)) % eol, result))
    {
        throw std::runtime_error("Unable to read/parse index file ");
    }

    return result; // http://en.wikipedia.org/wiki/Return_value_optimization
}

int main()
{
    index_t index = read_index("input.txt");

    using namespace karma;
    std::cout << format(('[' << int_ << ": " << *char_ << ']') % eol, index) << std::endl;

    for (int i=0; i<10; i++)
    {
        int lookup=rand()%6;
        std::cout << "Random lookup: " << lookup << ": " << index[lookup] << std::endl;
    }

    return 0;
}

这可能没有什么帮助,除了我;我开始练习使用精神解析和业力输出生成作为奖励:

#include <boost/spirit/include/karma.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/include/std_pair.hpp>
#include <fstream>
#include <map>

namespace qi = ::boost::spirit::qi;
namespace karma = ::boost::spirit::karma;

typedef std::map<int, std::string> index_t;

index_t read_index(const char* filename)
{
    using boost::spirit::istream_iterator;
    using namespace qi;

    index_t result;
    std::ifstream ifs(filename);
    ifs.unsetf(std::ios::skipws);
    istream_iterator begin(ifs), end;

    if (!parse(begin, end, (int_ >> omit[lit(',') >> *char_(" \t")] >> *(char_ - eol)) % eol, result))
    {
        throw std::runtime_error("Unable to read/parse index file ");
    }

    return result; // http://en.wikipedia.org/wiki/Return_value_optimization
}

int main()
{
    index_t index = read_index("input.txt");

    using namespace karma;
    std::cout << format(('[' << int_ << ": " << *char_ << ']') % eol, index) << std::endl;

    for (int i=0; i<10; i++)
    {
        int lookup=rand()%6;
        std::cout << "Random lookup: " << lookup << ": " << index[lookup] << std::endl;
    }

    return 0;
}

这有关系吗?对于那些知道C++的人来说,这应该很简单,但是我没有……是的,这很重要!所以这不是一个给我写代码的服务,而是一个问答网站。如果你先告诉我们你到目前为止尝试了什么以及为什么不奏效,我们可以提出具体的改进建议。好吧。。。Q1:如何在C++中打开文本文件?问题2:如何逐行读取文件?Q3:如何将字符串与基于该分隔符的数组转换为Q1、Q2:C++输入/输出文件:Q3:这有关系吗?对于那些知道C++的人来说,这应该很简单,但是我没有……是的,这很重要!所以这不是一个给我写代码的服务,而是一个问答网站。如果你先告诉我们你到目前为止尝试了什么以及为什么不奏效,我们可以提出具体的改进建议。好吧。。。Q1:如何在C++中打开文本文件?问题2:如何逐行读取文件?Q3:如何将字符串与基于该定界符的数组进行对齐?Q1、Q2:C++输入/输出与文件:Q3:引用Oli Charlesworth:所以不是给我写一些代码服务,它是没有帮助的,干扰的还是错误的?或者,到底是什么原因导致投票被否决?这样的行为使人们不再回答任何问题……这是没有帮助的,因为OP不会从向他提供完整的解决方案中学到很多东西,而问题本身是如此微不足道,只适合学习。让他思考自己真正的问题并回答这些问题更有用。我也有过类似的问题,这篇文章实际上很有帮助。让那些有问题的人决定什么是有用的,什么是不有用的,而不是让他们符合我们所认同的任何教育理论。如果有人愿意花时间给我写一些代码,实际上是有用的代码,他们应该为此受到表扬。不是说引导人们逐行寻找答案是一种不好的方法,但这不是唯一正确的方法。引用奥利·查尔斯沃思的话:那么,写我一些代码服务不是一种没有帮助、令人不安还是错误的服务吗?或者,到底是什么原因导致投票被否决?这样的行为使人们不再回答任何问题……这是没有帮助的,因为OP不会从向他提供完整的解决方案中学到很多东西,而问题本身是如此微不足道,只适合学习。让他思考自己真正的问题并回答这些问题更有用。我也有过类似的问题,这篇文章实际上很有帮助。让那些有问题的人决定什么是有用的,什么是不有用的,而不是让他们符合我们所认同的任何教育理论。如果有人愿意花时间给我写一些代码,实际上是有用的代码,他们应该为此受到表扬。不是说引导人们一行一行地找到答案是不好的方法,但这不是唯一正确的方法。