简体   繁体   English

从文件中读取数据并将数据存储在 map c++​​ 中

[英]To read from file and store data in map c++

I am trying to read file contents and store it in map data structure, where I am trying to skip comment section not be included in my map.我正在尝试读取文件内容并将其存储在地图数据结构中,我试图跳过不包含在我的地图中的评论部分。 I am unable to keep my map as expected, I am kind off clueless how to go ahead.我无法按预期保持我的地图,我有点不知道如何继续。 I have mentioned how my map should look like.我已经提到了我的地图应该是什么样子。 Please suggest me what changes I have to add in my code.请建议我必须在代码中添加哪些更改。 Thanks in Advnc :)感谢Advnc :)

Input file:输入文件:

# Filename: MyFile
# Revision: 107

Items              Types       count     price
snacks             junkfood
Mango              fruit        5         50
strawbery          fruit        10        50
carrot             veggie
burger             junkfood
beetroot           veggie       4         20
cinnamon           masala

Expected output file: Stored in map<string, string> [key] [value]预期的输出文件:存储在map<string, string> [key] [value]

Items      ->        Types       count     price
snacks     ->        junkfood
Mango      ->        fruit        5         50
strawbery  ->        fruit        10        50
carrot     ->        veggie
burger     ->        junkfood
beetroot   ->        veggie       4         20
cinnamon   ->        masala

CPP file: CPP 文件:

#include <map>
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <iterator>

using namespace std;

typedef std::pair<std::string, std::string> attribute_pair;

void check(ifstream &inputFile, map<string, string> &diffm)
{
    diffm.clear();
    std::string line;
    while (getline(inputFile, line))
    {
       std::stringstream ss(line);
       attribute_pair attribute;

       while (ss >> attribute.first >> attribute.second)
       {
          if (attribute.first != "#")
               diffm[attribute.first] = attribute.second;
       }
    }
}

int main(int argc, char const *argv[])
{
    map<string, string> list1;
     
    if (argc >= 2)
    {
        std::ifstream inputFile(argv[1]);
        check(inputFile, list1);

        map<string, string>::iterator itr;
        for (itr = list1.begin(); itr != list1.end(); itr++)
        {
           cout << itr->first << "     " << itr->second << "\n";
        }
    }
    else
    {
        std::cerr << "Usage <prog name here> <filename1 here> <filename2 here>\n";
        return -2;
    }
}

I'm feeling generous, I am guessing somewhat at your requirements (particularly how the value part of the map is meant to be).我感觉很慷慨,我在某种程度上猜测您的要求(特别是地图的价值部分应该如何)。 I have not tested this code.我没有测试过这段代码。

void check(ifstream &inputFile, map<string, string> &diffm)
{
    diffm.clear();
    std::string line;
    while (getline(inputFile, line))
    {
       if (line.empty() || line[0] == '#')
       {
           // skip blank or comment lines
       }
       else
       {
           std::stringstream ss(line);
           std::string key, value;
           // read first string (key)
           ss >> key;
           // read rest of line (value)
           getline(ss, value);
           diffm[key] = value;
       }
    }
}

This is a mistake这是个错误

while (getline(inputFile, line, '\0'))

should be应该

while (getline(inputFile, line))

The first one reads 'lines' terminated by the \0 character, which I seriously doubt is what you want.第一个读取由\0字符终止的“行”,我严重怀疑这是您想要的。 You want lines terminated by the \n character, that's what the second version does.您想要以\n字符终止的行,这就是第二个版本所做的。

This illustrates the point that Beta was making.这说明了 Beta 提出的观点。 The very first thing you try is bugged, so writing and testing the rest of the code is a waste of time.您尝试的第一件事是错误的,因此编写和测试其余代码是浪费时间。 You should write a little piece of code (like checking that you can read a file one line at a time) and only when you are sure that is working should you move onto to the rest of your task.您应该编写一小段代码(例如检查您是否可以一次读取一行文件),并且只有当您确定它正在工作时,您才应该继续执行其余的任务。 For instance, after working out how to read the file one line at a time, the next task should be to see if you can skip the comment lines.例如,在弄清楚如何一次读取一行文件之后,下一个任务应该是看看您是否可以跳过注释行。 See?看? Break the task into smaller tasks and solve each smaller task before moving onto the next .将任务分解成更小的任务并解决每个更小的任务,然后再进行下一个任务 That is vitally important.这是至关重要的。 Solving doesn't just mean writing and compiling the code, it means writing the code and writing some code to check that it is working (like some std::cout << ... code).解决不仅仅意味着编写和编译代码,还意味着编写代码编写一些代码来检查它是否正常工作(比如一些std::cout << ...代码)。

Always make baby steps (especially when you are learning).始终迈出小步(尤其是在学习时)。 There's too many things that can go wrong for you to write 20 lines of code at once.一次编写 20 行代码可能会出错的地方太多了。 Write two, three, or four lines of code, test that code and only when it is working write some more code.编写两行、三行或四行代码,测试该代码,然后仅在它工作时再编写一些代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM