Map file

I have a file which stores values in the following format

filename id path

For example :
..\..\include1 abc\common.h 4223 \ab1 abc\temp
..\..\include2\conhigh.h 4224 \vb12\temp

I would like to know what is the best way to retrieve values from this file .Can i accomplish this using hashmap ?I am looking for actual code ?

Who is Participating?
>>>> I think you have to define your fileformat at bit more precise

hth is right, but I will make the following assumptions:

- the second path always starts with a backslash
- the id number between two path strings is separated exactly by one space character

Regards, Alex

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <map>
using namespace std;

struct  Record
    int     id;
    string path1;
    string path2;
    Record() : id(0) {}
    Record(int i, const string& p1, const string& p2)
        : id(i), path1(p1), path2(p2) {}

int main()
    ifstream ifs("files.txt");
    string line;
    map<int, Record> recordMap;
    while (getline(ifs, line))
        // look for space + backslash
        int pos2 = line.find(" \\");
        // id found?
        if (pos2 == string::npos || pos2 < 2)
            continue;  // ignore line
        int pos1 = line.rfind(' ', pos2-1);
        if (pos1 == string::npos || pos1 < 2 || pos1 == pos2-1)
            continue;  // ignore line
        istringstream iss(line.substr(pos1+1, pos2-pos1+1));
        int id;
        iss >> id;
        if ( || recordMap.find(id) != recordMap.end())
            continue;   // ignore line
        recordMap[id] = Record(id, line.substr(0, pos1), line.substr(pos2+1));
    return 0;

Hi rashmi_be,
You can use INI API functions.
See GetPrivateProfileString

David Maisonave :-)

Are the id's unique and do I see multiple path's in the line?

If they are unique (well even if they aren't) you could either use a map or a hash_map to associate the id with the rest of the data.

I'd probably suggest a structure to hold the filename and paths.

typedef struct map_section{
   int id;
  std::string filename;
  std::vector< std::string > paths;
} map_section;

std::map< int, MAP_SECTION > map_file;

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Since the filename/path's in the file isn't enclosed in quotes, you just have to create some sort of line parser.

eg. there isn't anything invalidating such entries
..\folder 123\file 1234 path
and there is no standard way to parse this

unless the entries are separated by tab, you infact has a unparsable format
123 123 123 123

what is what? keep in mind that files and folders can consist of just numbers and there is the possibility of spaces in file and folder names.

I think you have to define your fileformat at bit more precise

you got a draft there, but it will break if the filename (first path) contain space + backslash ( a bit "kinky", but possible )

I would suggest to separate the fields by tab in the file, and then rework the solution from  itsmeandnobodyelse to just do line.find( '\t', pos )

>>>> if the filename (first path) contain space + backslash

folders with trailings spaces are not valid or better trailing spaces are truncated after folder creation:

     mkdir "test   "
     chdir test

Both statements are valid.

Regards, Alex

a C grade for a 20 points question where you got a full and complete solution is absolutely inacceptable. A C grade is appropriate if you don't get an answer or if experts refuse to explain their comments. I doubt that anybody will answer to your next question. I definitively won't.
B is the new A and C is the new B.  A complete answer only gets a B these days.

It's not even there for a big reason -- people take is so seriously.

This user's grading history is very misleading.
At first glance, it looks like the questioner has a good grading history, but when you look at each question, you'll notice that the only time an A grade has been awarded is when it has been force accepted by a Moderator.

So in reality, not only does this questioner have a bad grading history, but he/she has a bad history of abandoning questions.

The questioner also has a bad history of awarding very low points.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.