0

While I am running the code, the File(.csv) transfer the data into the map but the first line prints as the last line in Map. Even I am able to find only the last key in the file skipping all other keys in the Map.

Data in my file:- (file.csv)

9956,Aus
9955,Aus
A03A1,Bzl
A03B1,Bzl

I have tried the same code for Windows Environment and the code is running fine. Need to execute the same in Ubuntu Environment.

    #include <fstream>
    #include <map>
    #include <utility>
    #include <vector>
    #include <iostream>
    #include <string>
    #include <algorithm>

    using namespace std;

    int main(){
    ifstream myfile;
    myfile.open("file.csv");

    std::multimap<std::string, std::string> myMap; 

    string key;
    string value;

    if(myfile)
   {//file opened
     while (!myfile.eof())
    {
     getline(myfile,key,','); 
     myfile>>value; //read the next value from the file

     key.erase(std::remove(key.begin(), key.end(), '\n'), key.end());
     myMap.insert(pair <string, string> (key, value));
    }
   }
     multimap<string, string>::iterator it;
     for(it=myMap.begin();it!=myMap.end(); ++it)
    cout<<it->first<<"=>"<<it->second<<endl; //display the key/value pair

     string input;
     cout << "Enter the code: ";
     cin >> input;
     it = myMap.find(input);   //user input stored in it
     if (it != myMap.end()) //compare the input with the keys
     {
        cout << " The code belongs to = " << it->second << endl; 
     } 
     else
     {
       cout << "The code is not available " << endl;                   
     }

      myfile.close();
      return 0;
     }

Actual Output:-

     9955=>Aus
     A03A1=>Bzl
     A03B1=>Bzl
     9956=>Aus
      Enter the code: A03A1
      The code is not available.

Expected Output:-

      9956=>Aus
      9955=>Aus
      A03A1=>Bzl
      A03B1=>Bzl
      Enter the code: A03A1
      The code is belongs to Bzl
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • [SO: Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/q/5605125/7478597) – Scheff's Cat Aug 09 '19 at 08:55
  • First things first... Please read [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) Then don't try to parse CSV files yourself, get a library to do it for you. CSV files are *deceptively* simple, but could have a lot of odd corner cases that makes it non-trivial. – Some programmer dude Aug 09 '19 at 08:57
  • There's also some weird things in your code, for example when you remove newlines from `key`, but unless there's an actual newline in the file in the middle of the "key" value, there won't be any newlines in `key`. – Some programmer dude Aug 09 '19 at 08:58
  • @Scheff- The same code is running fine in Windows Environment. Facing issue in linux/ubuntu environment only. – nscontact Aug 09 '19 at 09:26
  • My crystal ball says your CSV file contains windows line endings, which would cause a stray `'\r'` to end up in your `key`. If that is the case, running `dos2unix` on your csv file should fix it. A more structural solution is to use `getline` (with no separator or `'\n'` as separator) to read your file line by line and strip off the `'\r'` before processing the line. – Botje Aug 09 '19 at 09:57
  • @Botje... Thanks. It worked as per the requirement. Just create the file using VI Editor. – nscontact Aug 09 '19 at 11:41
  • Do take the time to rework your file reading approach. What you are currently doing with `key` is very unorthodox. You should process your file line by line instead. – Botje Aug 09 '19 at 11:43

0 Answers0