string to vector<string> and splitting a string. Need help!

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2009
Posts: 48
Reputation: Argo54325 is an unknown quantity at this point 
Solved Threads: 0
Argo54325 Argo54325 is offline Offline
Light Poster

string to vector<string> and splitting a string. Need help!

 
0
  #1
Mar 31st, 2009
Hello, I'm getting the following error with this code, also i would like to know how to add the path and filespec together with a and be able to split the 2 so it would be 3 args not 4.

ERROR:

error C2664: 'std::vector<_Ty>::vector(const std::allocator<_Ty> &)' : cannot convert parameter 1 from 'char *' to 'const std::allocator<_Ty> &'


This is my current code:

  1. #include <iomanip>
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5. using namespace std;
  6.  
  7. #include <boost/filesystem.hpp>
  8. #include <boost/filesystem/fstream.hpp>
  9. using namespace boost::filesystem;
  10.  
  11. #include <boost/regex.hpp>
  12.  
  13. void show_files( const path & directory, bool recurse_into_subdirs = false )
  14. {
  15. if( exists( directory ) )
  16. {
  17. directory_iterator end ;
  18. for( directory_iterator iter(directory) ; iter != end ; ++iter )
  19. if ( is_directory( *iter ) )
  20. {
  21. cout << "\nd " << iter->path() ;
  22. if( recurse_into_subdirs ) show_files(*iter) ;
  23. }
  24. else
  25. cout << "\n- " << iter->path() ;
  26. }
  27. }
  28.  
  29. int main(int argc, char *argv[])
  30. {
  31. if (argc != 4)
  32. {
  33. cout << "Usage: program.exe [switches] [path] [filespec]" << endl;
  34. }
  35. else
  36. {
  37. vector<string> s_switches(argv[1]);
  38. string s_path(argv[2]);
  39. string s_filespec(argv[3]);
  40.  
  41. cout << s_switches[1] << endl;
  42.  
  43. bool aSwitch = false;
  44. bool rSwitch = false;
  45. bool RSwitch = false;
  46. bool lSwitch = false;
  47.  
  48. //set switches
  49. if (s_switches[1] == "-a")
  50. {
  51. bool aSwitch = true;
  52. }
  53. if (s_switches[1] == "-r")
  54. {
  55. bool rSwitch = true;
  56. }
  57. if (s_switches[1] == "-R")
  58. {
  59. bool RSwitch = true;
  60. }
  61. if (s_switches[1] == "-l")
  62. {
  63. bool lSwitch = true;
  64. }
  65.  
  66. //boost::regex expr(s_filespec);
  67. //fs::path currentPath(s_path);
  68. //cout << currentPath << endl;
  69. //fs::basic_directory_iterator(s_path);
  70.  
  71. show_files(s_path) ;
  72.  
  73. }
  74.  
  75. cin.ignore();
  76. return 0;
  77. }
Last edited by Argo54325; Mar 31st, 2009 at 8:37 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 181
Reputation: adam1122 is an unknown quantity at this point 
Solved Threads: 28
adam1122 adam1122 is offline Offline
Junior Poster

Re: string to vector<string> and splitting a string. Need help!

 
0
  #2
Mar 31st, 2009
Instead of:
  1. vector<string> s_switches(argv[1]);
try:
  1. vector<string> s_switches(string(argv[1]));
Last edited by adam1122; Mar 31st, 2009 at 8:53 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 48
Reputation: Argo54325 is an unknown quantity at this point 
Solved Threads: 0
Argo54325 Argo54325 is offline Offline
Light Poster

Re: string to vector<string> and splitting a string. Need help!

 
0
  #3
Mar 31st, 2009
ok that works, but now i get errors for things like, s_switches[1]

error C2109: subscript requires array or pointer type
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 181
Reputation: adam1122 is an unknown quantity at this point 
Solved Threads: 28
adam1122 adam1122 is offline Offline
Junior Poster

Re: string to vector<string> and splitting a string. Need help!

 
0
  #4
Mar 31st, 2009
That line isn't going to work as you want it to. It's not going to pull in all of the "switch" arguments.

You're using Boost already. Why not use program options (http://www.boost.org/doc/libs/1_38_0...m_options.html) from Boost?
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 48
Reputation: Argo54325 is an unknown quantity at this point 
Solved Threads: 0
Argo54325 Argo54325 is offline Offline
Light Poster

Re: string to vector<string> and splitting a string. Need help!

 
0
  #5
Mar 31st, 2009
I have no idea how to use that library, this is for a school project and we haven't used that library before. I tried to use getline() but can't get that to work either. there are a max of 4 switches (-a, -r, -R, -l)

Any ideas?
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 181
Reputation: adam1122 is an unknown quantity at this point 
Solved Threads: 28
adam1122 adam1122 is offline Offline
Junior Poster

Re: string to vector<string> and splitting a string. Need help!

 
0
  #6
Mar 31st, 2009
I would encourage you to look into it for the future but I can understand you probably just want to get this done on schedule.

One solution would be to not assume that there are 4 arguments, because you have 4 optional switches and 2 other arguments, in addition to the program name.

Since the last two arguments are mandatory, you access them as the last argument in the argv and the second to last argument in argv . The switch arguments are then everything from argv[1] to argv[argc-3]
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 48
Reputation: Argo54325 is an unknown quantity at this point 
Solved Threads: 0
Argo54325 Argo54325 is offline Offline
Light Poster

Re: string to vector<string> and splitting a string. Need help!

 
0
  #7
Mar 31st, 2009
SO you saying to have more then 4 args? my prof only wants 3, the switches should be -r-R-l for example and the next arg should be C:\program files (x86)\.... (then a character that would separate the 2, i chose |) followed by the regular expression, eg. (B|b)ill.*

All i need to know for now is how to separate them, once i can get that done i can work on using what has been specified.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 181
Reputation: adam1122 is an unknown quantity at this point 
Solved Threads: 28
adam1122 adam1122 is offline Offline
Junior Poster

Re: string to vector<string> and splitting a string. Need help!

 
0
  #8
Mar 31st, 2009
I see, they made it a little easier for you.

Just treat the switches as a string like you do with the other arguments. Forget the vector.

You then just parse the string of switches. One solution would be to use a for loop and the substr method of string .

Your loop could look like this:

  1. for(int x=0; x < s_switches.size(); x+=2)

I'll leave the body of the loop for you since I probably shouldn't do your hw. But what you would want to do is access the element at x and x+1 to get a single switch (e.g. -r). You can get parts of the string with substr. A reference is here: http://www.cplusplus.com/reference/string/string/

Also note that if the size of the switch isn't divisible by 2, it's not valid. You might want to check for this before the loop.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC