Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 214
  • Last Modified:

reference file or reference value

Hi experts,
my input file is like this:

A         B
1         2
AA      BB

my refernece file is like this:

A      B       C
1      2       3
AA    BB     CC

I want to return
C
3
CC
on my output file

I am going to do
A-B
1-2
AA-BB

and

A-B C
1-2 3
AA-BB CC
in my reference file and my input file and compare them, if match then return value

So, I have the followings
typedef struct
{
       char keydata[100];
       char valuedata[100];
}keyref;

char* findkeyvalue( keyref* ref, const char* k)
{
         for(int i=0;i <MaxSize;i++)
         {
              if (!strcmp(ref[i].keydata, k))
                     return ref[i].valuedata;
         }
          return "NONE"
};

main()
{
     ifstream fin( myinputfile);
     ifstream fin1( myreferencefile);
     keyfer refer_data[MaxSize];
     string refer_read;
     int k=0;
     getline(fin1, refer_read)
     while( getline (fin1, refer_read))
     {
          vector<string> refer_row_elem;
          Getfields(refer_read1, refer1_row_elem, ',');
          refer_row_elem[0] += "-";
          refer_row_elem[0] += refer_row_elem[1];
          strcpy(refe_data[k].keydata, refer_row_elem[0].c_str());
          strcpy(refe_data[k].valuedata, refer_row_elem[2].c_str());
          k++;
       }

       while( getline (fin, row_read))
     {
          vector<string> row_elem;
          Getfields(row_read, row_elem, ',');
          row_elem[0] += "-";
          row_elem[0] += row_elem[1];
       }

       char * valuefound;
       valuefound = findkeyvalue(refer_data, row_elem[0].c_str());  
}
It returns NONE, no match found, Whats wrong ?
If I compare just 2 columns, no +=, then  I have match.
But after +=, then no match found.

Thanks in advance
0
justinY
Asked:
justinY
  • 3
1 Solution
 
rstaveleyCommented:
You can do this with a map, which has a pair of strings as its key. Here's how:
--------8<--------
#include <iostream>
#include <fstream>
#include <sstream>
#include <map>

int main()
{
      typedef std::pair<std::string,std::string> Key;
      typedef std::map<Key,std::string> Map;

      std::string line;
      Map map;

      // Load the map using the reference file
      {
            std::ifstream fref("ref.dat");
            while (getline(fref,line)) {
                  std::istringstream is(line);
                  std::string value;
                  Key key;
                  if (is >> key.first >> key.second >> value)
                        map[key] = value;
            }
      }

      // Load the map using the reference file
      {
            std::ifstream fkey("key.dat");
            std::ofstream fout("out.dat");
            while (getline(fkey,line)) {
                  std::istringstream is(line);
                  Key key;
                  if (is >> key.first >> key.second)
                        fout << map[key] << '\n';
            }
      }
}
--------8<--------
0
 
rstaveleyCommented:
Second comment should read:

// Load the map using the key file and generate the output file
0
 
rstaveleyCommented:
I'll try that again... Second comment should read:

/* Read through key file and generate the output file doing look-ups on the map (assumes all keys have values in the reference file missing values will show as blank lines) */
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now