Home > Back-end >  To read from file and store data in map c
To read from file and store data in map c

Time:06-26

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 :)

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]

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:

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

CodePudding user response:

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

CodePudding user response:

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. You want lines terminated by the \n character, that's what the second version does.

This illustrates the point that Beta was making. 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).

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. Write two, three, or four lines of code, test that code and only when it is working write some more code.

  • Related