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
//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. -
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. -
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?
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()); } } }
-
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.Hint - you haven't got
ls
in your map!!!! You've gotdir
, which is the WIndows equivalent, but notls
. When you addls,/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_
-
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()); } } }
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
-
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()); } } }
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
-
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()); } } }
-
This code works fine on Windows. I cannot see what you are doing wrong, but it may be as mentioned by Stuart below.
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
-
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
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