Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. STL map in UNIX

STL map in UNIX

Scheduled Pinned Locked Moved C / C++ / MFC
algorithmshelpc++linux
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ashukasama
    wrote on last edited by
    #1

    I want to write shell programming in UNIX, alao i am new to unix. i have written very simple program for console ISSUE: I added some item in map as string int pair, now i am searching string read from commandline and i am unable to find it, but if i use same code with windows . i am able to do same. Code is as follows

    //Inclue files for libraries
    #include <stdlib.h>
    #include <ctype.h>
    #include <iostream>
    #include <fstream>
    #include <iostream>
    #include <iomanip>
    #include <ctime>
    #include <string>
    #include <stdio.h>
    #include <map>
    #include <algorithm>

    using namespace std;

    map <string, int> m1;

    int main(int argc, char* argv[])
    {
    m1.insert(map <string, int> :: const_iterator::value_type("cd",0));
    m1.insert(map <string, int> :: const_iterator::value_type("dir",0));
    m1.insert(map <string, int> :: const_iterator::value_type("pwd",0));
    m1.insert(map <string, int> :: const_iterator::value_type("echo",0));
    m1.insert(map <string, int> :: const_iterator::value_type("help",0));
    m1.insert(map <string, int> :: const_iterator::value_type("quit",0));
    map <string, int> :: const_iterator m1_AcIter, m1_RcIter;
    typedef pair <string, int> Int_Pair;

    if(argc == 1)
    {
    	cout<<"Please enter unix command..";
    }
    else
    {
    	string command;
    	command =  argv\[1\];            
    	map <string, int> :: const\_iterator m1\_AcIter, m1\_RcIter;
    	m1\_AcIter   = m1.find(command);
    	if(m1\_AcIter  == m1.end())
    	{
    		cout<<"Didn't find unix command - "<< command<<endl ;
    	}
    	else
    	{ 
    		system(command.c\_str());
    	}
    
    }
    
    map <string, int> :: iterator it;
    for ( it=m1.begin() ; it != m1.end(); it++ )
    	cout << (\*it).first << " => " << (\*it).second << endl;
    
    return 0;
    

    }

    now i am running this code ./a.out ls and i received following output Didn't find unix command - ls cd =>; 0 dir =>; 0 echo =>; 0 help =>; 0 pwd =>; 0 quit =>; 0 but same code is working perfectly with windows.

    L S 2 Replies Last reply
    0
    • A ashukasama

      I want to write shell programming in UNIX, alao i am new to unix. i have written very simple program for console ISSUE: I added some item in map as string int pair, now i am searching string read from commandline and i am unable to find it, but if i use same code with windows . i am able to do same. Code is as follows

      //Inclue files for libraries
      #include <stdlib.h>
      #include <ctype.h>
      #include <iostream>
      #include <fstream>
      #include <iostream>
      #include <iomanip>
      #include <ctime>
      #include <string>
      #include <stdio.h>
      #include <map>
      #include <algorithm>

      using namespace std;

      map <string, int> m1;

      int main(int argc, char* argv[])
      {
      m1.insert(map <string, int> :: const_iterator::value_type("cd",0));
      m1.insert(map <string, int> :: const_iterator::value_type("dir",0));
      m1.insert(map <string, int> :: const_iterator::value_type("pwd",0));
      m1.insert(map <string, int> :: const_iterator::value_type("echo",0));
      m1.insert(map <string, int> :: const_iterator::value_type("help",0));
      m1.insert(map <string, int> :: const_iterator::value_type("quit",0));
      map <string, int> :: const_iterator m1_AcIter, m1_RcIter;
      typedef pair <string, int> Int_Pair;

      if(argc == 1)
      {
      	cout<<"Please enter unix command..";
      }
      else
      {
      	string command;
      	command =  argv\[1\];            
      	map <string, int> :: const\_iterator m1\_AcIter, m1\_RcIter;
      	m1\_AcIter   = m1.find(command);
      	if(m1\_AcIter  == m1.end())
      	{
      		cout<<"Didn't find unix command - "<< command<<endl ;
      	}
      	else
      	{ 
      		system(command.c\_str());
      	}
      
      }
      
      map <string, int> :: iterator it;
      for ( it=m1.begin() ; it != m1.end(); it++ )
      	cout << (\*it).first << " => " << (\*it).second << endl;
      
      return 0;
      

      }

      now i am running this code ./a.out ls and i received following output Didn't find unix command - ls cd =>; 0 dir =>; 0 echo =>; 0 help =>; 0 pwd =>; 0 quit =>; 0 but same code is working perfectly with windows.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      I don't see a problem here. Your map table does not contain an entry for 'ls' so the program correctly reports it as not being found. Quite how this works on Windows remains a mystery - can you show the working sample?

      A 1 Reply Last reply
      0
      • L Lost User

        I don't see a problem here. Your map table does not contain an entry for 'ls' so the program correctly reports it as not being found. Quite how this works on Windows remains a mystery - can you show the working sample?

        A Offline
        A Offline
        ashukasama
        wrote on last edited by
        #3

        Above code is working, But this code is not working

        //Inclue files for libraries
        #include <stdlib.h>
        #include <ctype.h>
        #include <iostream>
        #include <fstream>
        #include <iostream>
        #include <iomanip>
        #include <ctime>
        #include <string>
        #include <stdio.h>
        #include <map>
        #include <algorithm>

        using namespace std;

        map <string, int> m1;
        string tokenizeReturnFirst(const string& str,const char& delimiters)
        {
        string token;

        // skip delimiters at beginning.
        string::size\_type lastPos = str.find\_first\_not\_of(delimiters, 0);
        
        // find first "non-delimiter".
        string::size\_type pos = str.find\_first\_of(delimiters, lastPos);
        if( pos!=string::npos)
        {
            token = str.substr(lastPos, pos - lastPos);
        }
        else
        {
            token = str;
        }
        
        
        return token;
        

        }

        int main()
        {
        m1.insert(map <string, int> :: const_iterator::value_type("cd",0));
        m1.insert(map <string, int> :: const_iterator::value_type("dir",0));
        m1.insert(map <string, int> :: const_iterator::value_type("pwd",0));
        m1.insert(map <string, int> :: const_iterator::value_type("echo",0));
        m1.insert(map <string, int> :: const_iterator::value_type("help",0));
        m1.insert(map <string, int> :: const_iterator::value_type("quit",0));
        map <string, int> :: const_iterator m1_AcIter, m1_RcIter;
        typedef pair <string, int> Int_Pair;
        ifstream ifile( "test.bat");
        if ( ifile.fail() )
        {
        fprintf(stderr, "Cannot open %s\n", "output_file");
        return 0;
        }

        while (!ifile.eof())
        {
            string data\_string ;
            getline(ifile,data\_string);
            string command = tokenizeReturnFirst(data\_string,string::value\_type(' '));
            //std::transform( command.begin(), command.end(), command.begin(), ::tolower );
            cout<<"Searched string - "<<command <<endl;
            m1\_AcIter   = m1.find(command);
            if(!command.empty())        
            {
                if(m1\_AcIter  == m1.end())
                {
                    cout<<"Didn't find unix command - "<< data\_string<<endl ;
                }
                else
                { 
                    cout<<"SYSTEM - "<< data\_string.c\_str()<<endl;
                    system(data\_string.c\_str());
                }
            }
        }
        
        S L 3 Replies Last reply
        0
        • A ashukasama

          I want to write shell programming in UNIX, alao i am new to unix. i have written very simple program for console ISSUE: I added some item in map as string int pair, now i am searching string read from commandline and i am unable to find it, but if i use same code with windows . i am able to do same. Code is as follows

          //Inclue files for libraries
          #include <stdlib.h>
          #include <ctype.h>
          #include <iostream>
          #include <fstream>
          #include <iostream>
          #include <iomanip>
          #include <ctime>
          #include <string>
          #include <stdio.h>
          #include <map>
          #include <algorithm>

          using namespace std;

          map <string, int> m1;

          int main(int argc, char* argv[])
          {
          m1.insert(map <string, int> :: const_iterator::value_type("cd",0));
          m1.insert(map <string, int> :: const_iterator::value_type("dir",0));
          m1.insert(map <string, int> :: const_iterator::value_type("pwd",0));
          m1.insert(map <string, int> :: const_iterator::value_type("echo",0));
          m1.insert(map <string, int> :: const_iterator::value_type("help",0));
          m1.insert(map <string, int> :: const_iterator::value_type("quit",0));
          map <string, int> :: const_iterator m1_AcIter, m1_RcIter;
          typedef pair <string, int> Int_Pair;

          if(argc == 1)
          {
          	cout<<"Please enter unix command..";
          }
          else
          {
          	string command;
          	command =  argv\[1\];            
          	map <string, int> :: const\_iterator m1\_AcIter, m1\_RcIter;
          	m1\_AcIter   = m1.find(command);
          	if(m1\_AcIter  == m1.end())
          	{
          		cout<<"Didn't find unix command - "<< command<<endl ;
          	}
          	else
          	{ 
          		system(command.c\_str());
          	}
          
          }
          
          map <string, int> :: iterator it;
          for ( it=m1.begin() ; it != m1.end(); it++ )
          	cout << (\*it).first << " => " << (\*it).second << endl;
          
          return 0;
          

          }

          now i am running this code ./a.out ls and i received following output Didn't find unix command - ls cd =>; 0 dir =>; 0 echo =>; 0 help =>; 0 pwd =>; 0 quit =>; 0 but same code is working perfectly with windows.

          S Offline
          S Offline
          Stuart Dootson
          wrote on last edited by
          #4

          Hint - you haven't got ls in your map!!!! You've got dir, which is the WIndows equivalent, but not ls. When you add ls,/code>, it works. I know, I've just run it on OS X, which is a Unix... _Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p_

          1 Reply Last reply
          0
          • A ashukasama

            Above code is working, But this code is not working

            //Inclue files for libraries
            #include <stdlib.h>
            #include <ctype.h>
            #include <iostream>
            #include <fstream>
            #include <iostream>
            #include <iomanip>
            #include <ctime>
            #include <string>
            #include <stdio.h>
            #include <map>
            #include <algorithm>

            using namespace std;

            map <string, int> m1;
            string tokenizeReturnFirst(const string& str,const char& delimiters)
            {
            string token;

            // skip delimiters at beginning.
            string::size\_type lastPos = str.find\_first\_not\_of(delimiters, 0);
            
            // find first "non-delimiter".
            string::size\_type pos = str.find\_first\_of(delimiters, lastPos);
            if( pos!=string::npos)
            {
                token = str.substr(lastPos, pos - lastPos);
            }
            else
            {
                token = str;
            }
            
            
            return token;
            

            }

            int main()
            {
            m1.insert(map <string, int> :: const_iterator::value_type("cd",0));
            m1.insert(map <string, int> :: const_iterator::value_type("dir",0));
            m1.insert(map <string, int> :: const_iterator::value_type("pwd",0));
            m1.insert(map <string, int> :: const_iterator::value_type("echo",0));
            m1.insert(map <string, int> :: const_iterator::value_type("help",0));
            m1.insert(map <string, int> :: const_iterator::value_type("quit",0));
            map <string, int> :: const_iterator m1_AcIter, m1_RcIter;
            typedef pair <string, int> Int_Pair;
            ifstream ifile( "test.bat");
            if ( ifile.fail() )
            {
            fprintf(stderr, "Cannot open %s\n", "output_file");
            return 0;
            }

            while (!ifile.eof())
            {
                string data\_string ;
                getline(ifile,data\_string);
                string command = tokenizeReturnFirst(data\_string,string::value\_type(' '));
                //std::transform( command.begin(), command.end(), command.begin(), ::tolower );
                cout<<"Searched string - "<<command <<endl;
                m1\_AcIter   = m1.find(command);
                if(!command.empty())        
                {
                    if(m1\_AcIter  == m1.end())
                    {
                        cout<<"Didn't find unix command - "<< data\_string<<endl ;
                    }
                    else
                    { 
                        cout<<"SYSTEM - "<< data\_string.c\_str()<<endl;
                        system(data\_string.c\_str());
                    }
                }
            }
            
            S Offline
            S Offline
            Stuart Dootson
            wrote on last edited by
            #5

            Don't know what you're testing it on, but your code certainly works on Mac OS X 10.5.8.

            Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

            1 Reply Last reply
            0
            • A ashukasama

              Above code is working, But this code is not working

              //Inclue files for libraries
              #include <stdlib.h>
              #include <ctype.h>
              #include <iostream>
              #include <fstream>
              #include <iostream>
              #include <iomanip>
              #include <ctime>
              #include <string>
              #include <stdio.h>
              #include <map>
              #include <algorithm>

              using namespace std;

              map <string, int> m1;
              string tokenizeReturnFirst(const string& str,const char& delimiters)
              {
              string token;

              // skip delimiters at beginning.
              string::size\_type lastPos = str.find\_first\_not\_of(delimiters, 0);
              
              // find first "non-delimiter".
              string::size\_type pos = str.find\_first\_of(delimiters, lastPos);
              if( pos!=string::npos)
              {
                  token = str.substr(lastPos, pos - lastPos);
              }
              else
              {
                  token = str;
              }
              
              
              return token;
              

              }

              int main()
              {
              m1.insert(map <string, int> :: const_iterator::value_type("cd",0));
              m1.insert(map <string, int> :: const_iterator::value_type("dir",0));
              m1.insert(map <string, int> :: const_iterator::value_type("pwd",0));
              m1.insert(map <string, int> :: const_iterator::value_type("echo",0));
              m1.insert(map <string, int> :: const_iterator::value_type("help",0));
              m1.insert(map <string, int> :: const_iterator::value_type("quit",0));
              map <string, int> :: const_iterator m1_AcIter, m1_RcIter;
              typedef pair <string, int> Int_Pair;
              ifstream ifile( "test.bat");
              if ( ifile.fail() )
              {
              fprintf(stderr, "Cannot open %s\n", "output_file");
              return 0;
              }

              while (!ifile.eof())
              {
                  string data\_string ;
                  getline(ifile,data\_string);
                  string command = tokenizeReturnFirst(data\_string,string::value\_type(' '));
                  //std::transform( command.begin(), command.end(), command.begin(), ::tolower );
                  cout<<"Searched string - "<<command <<endl;
                  m1\_AcIter   = m1.find(command);
                  if(!command.empty())        
                  {
                      if(m1\_AcIter  == m1.end())
                      {
                          cout<<"Didn't find unix command - "<< data\_string<<endl ;
                      }
                      else
                      { 
                          cout<<"SYSTEM - "<< data\_string.c\_str()<<endl;
                          system(data\_string.c\_str());
                      }
                  }
              }
              
              S Offline
              S Offline
              Stuart Dootson
              wrote on last edited by
              #6

              What line terminations does the file have? \n or \r\n? It makes a difference - if you've produced the files in Windows, then they're likely to have \r\n. Unix systems will read the \r as part of the command string, so the command you're looking for will be (for example) 'pwd\r'. I've just tested that theory on my Mac (saved test.bat with Windows line-endings) and it reflects what you've seen.

              Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

              1 Reply Last reply
              0
              • A ashukasama

                Above code is working, But this code is not working

                //Inclue files for libraries
                #include <stdlib.h>
                #include <ctype.h>
                #include <iostream>
                #include <fstream>
                #include <iostream>
                #include <iomanip>
                #include <ctime>
                #include <string>
                #include <stdio.h>
                #include <map>
                #include <algorithm>

                using namespace std;

                map <string, int> m1;
                string tokenizeReturnFirst(const string& str,const char& delimiters)
                {
                string token;

                // skip delimiters at beginning.
                string::size\_type lastPos = str.find\_first\_not\_of(delimiters, 0);
                
                // find first "non-delimiter".
                string::size\_type pos = str.find\_first\_of(delimiters, lastPos);
                if( pos!=string::npos)
                {
                    token = str.substr(lastPos, pos - lastPos);
                }
                else
                {
                    token = str;
                }
                
                
                return token;
                

                }

                int main()
                {
                m1.insert(map <string, int> :: const_iterator::value_type("cd",0));
                m1.insert(map <string, int> :: const_iterator::value_type("dir",0));
                m1.insert(map <string, int> :: const_iterator::value_type("pwd",0));
                m1.insert(map <string, int> :: const_iterator::value_type("echo",0));
                m1.insert(map <string, int> :: const_iterator::value_type("help",0));
                m1.insert(map <string, int> :: const_iterator::value_type("quit",0));
                map <string, int> :: const_iterator m1_AcIter, m1_RcIter;
                typedef pair <string, int> Int_Pair;
                ifstream ifile( "test.bat");
                if ( ifile.fail() )
                {
                fprintf(stderr, "Cannot open %s\n", "output_file");
                return 0;
                }

                while (!ifile.eof())
                {
                    string data\_string ;
                    getline(ifile,data\_string);
                    string command = tokenizeReturnFirst(data\_string,string::value\_type(' '));
                    //std::transform( command.begin(), command.end(), command.begin(), ::tolower );
                    cout<<"Searched string - "<<command <<endl;
                    m1\_AcIter   = m1.find(command);
                    if(!command.empty())        
                    {
                        if(m1\_AcIter  == m1.end())
                        {
                            cout<<"Didn't find unix command - "<< data\_string<<endl ;
                        }
                        else
                        { 
                            cout<<"SYSTEM - "<< data\_string.c\_str()<<endl;
                            system(data\_string.c\_str());
                        }
                    }
                }
                
                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                This code works fine on Windows. I cannot see what you are doing wrong, but it may be as mentioned by Stuart below.

                A 1 Reply Last reply
                0
                • L Lost User

                  This code works fine on Windows. I cannot see what you are doing wrong, but it may be as mentioned by Stuart below.

                  A Offline
                  A Offline
                  ashukasama
                  wrote on last edited by
                  #8

                  yesh i resloved the issue. it is due to file is generated by windows so it contain /r/n so now i created file in unix system , now it resolved my issue. but how we handle such situation

                  S 1 Reply Last reply
                  0
                  • A ashukasama

                    yesh i resloved the issue. it is due to file is generated by windows so it contain /r/n so now i created file in unix system , now it resolved my issue. but how we handle such situation

                    S Offline
                    S Offline
                    Stuart Dootson
                    wrote on last edited by
                    #9

                    Add something straight after the getline call to remove a trailing \r? Like this:

                      if (\*data\_string.rbegin() == '\\r')
                         data\_string.erase(data\_string.length()-1);
                    

                    Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups