reference file or reference value

Posted on 2004-11-12
Last Modified: 2012-05-05
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
on my output file

I am going to do


1-2 3
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];

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"

     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());

       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
Question by:justinY
    LVL 17

    Accepted Solution

    You can do this with a map, which has a pair of strings as its key. Here's how:
    #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';
    LVL 17

    Expert Comment

    Second comment should read:

    // Load the map using the key file and generate the output file
    LVL 17

    Expert Comment

    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) */

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Article by: SunnyDark
    This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now