|
-
September 5th, 2009, 03:51 AM
#1
STL map in UNIX
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
Code:
/
//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 my input is
./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,
Last edited by ashukasama; September 5th, 2009 at 03:58 AM.
Reason: simplified code.
-
September 5th, 2009, 04:27 AM
#2
Re: STL map in UNIX
First, there is no such thing as an "STL map in UNIX". STL is part of the C++ standard library, so STL is part of a C++ compiler library, not an operating system.
Second:
Code:
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));
Where do you see "ls" inserted into the map? What output did you expect to see?
Regards,
Paul McKenzie
-
September 5th, 2009, 04:38 AM
#3
Re: STL map in UNIX
Thanks for your response,
But i wans not able to execute this code,
code:
Code:
//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());
}
}
}
return 0;
}
Input file test.bat file content
now i tried to execute
Input:
Output:
Searched string - ls
Didn't find unix command - ls
Searched string - pwd
Didn't find unix command - pwd
Searched string - echo
Didn't find unix command - echo
Searched string - aaa
Didn't find unix command - aaa
Searched string - quit
Didn't find unix command - quit
Searched string -
Last edited by ashukasama; September 5th, 2009 at 04:42 AM.
-
September 5th, 2009, 04:44 AM
#4
Re: STL map in UNIX
 Originally Posted by ashukasama
Thanks for your response,
But i wans not able to execute this code,
This is different than the code you originally posted.
Please do not start a thread with some code, and then all of a sudden, change the code to something else without saying anything about the original code you posted.
The original code you posted works as designed -- you did not store "ls" in a map, so of course it isn't going to be found.
Regards,
Paul McKenzie
-
September 5th, 2009, 04:49 AM
#5
Re: STL map in UNIX
yes paul , i changed my code, the my first code i posted is working, but this one is not wokring , but if you need i will start new thread. else please help me out.
thanks
Ashish
-
September 5th, 2009, 04:55 AM
#6
Re: STL map in UNIX
 Originally Posted by ashukasama
yes paul , i changed my code, the my first code i posted is working, but this one is not wokring , but if you need i will start new thread. else please help me out.
thanks
Ashish
Code:
string command = tokenizeReturnFirst(data_string,string::value_type(' '));
What is returned from this function? Does the string have trailing or leading spaces?
The string has to match exactly what you have placed in the map. This means no spaces at the end of the word.
Regards,
Paul McKenzie
-
September 5th, 2009, 05:02 AM
#7
Re: STL map in UNIX
Let's cut to the chase:
Code:
#include <map>
#include <string>
#include <iostream>
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));
if ( m1.find("pwd") != m1.end() )
cout << "We found pwd";
else
cout << "pwd was not found";
}
You see that this works. This means that your function is not returning the string you think it's returning. The string probably contains spaces or some other non-printable (invisible) control characters, and this is what is causing the find() to fail.
Regards,
Paul McKenzie
-
September 5th, 2009, 05:15 AM
#8
Re: STL map in UNIX
now I think issue is due to /r/n appended by windows, i tried to read this file with getline function which return string including /r. now i created new file itself in unix system. now its working
Thanks paul
Ashish
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|