1

Possible Duplicate:
Splitting a string in C++

I am doing client server programming using C++.

My client sends a string with the value

string receiveClient = "auth#user:pass";

How do I split the receiveClient variable by '#' and ':' as delimiters?


I have tried to use this function I found online

vector split (const string &s,char delim)
{
  vector string elems;
  return(s,delim,elems);
}

and I did this at main():

vector x = split(&receiveClient,"#");

But it return me the following

server.cpp: In function ‘int main()’:
server.cpp:128:8: error: missing template arguments before ‘x’
server.cpp:128:8: error: expected ‘;’ before ‘x’
root@ubuntu:/home/baoky/csci222_assn2# g++ server server.cpp
server.cpp:47:1: error: invalid use of template-name ‘std::vector’ without an argument list
server.cpp: In function ‘int main()’:
server.cpp:128:8: error: missing template arguments before ‘x’
server.cpp:128:8: error: expected ‘;’ before ‘x’

Thanks for all help. greatly appreciated

Community
  • 1
  • 1
user1587149
  • 87
  • 2
  • 7
  • Some of the answers here you may find useful: http://stackoverflow.com/questions/11374617/the-easiest-way-to-read-formatted-input-in-c – hmjd Aug 09 '12 at 10:05
  • 5
    I have no idea where you found that function online, but it's just completely useless because it doesn't compile, and it doesn't make sense. I recommend you avoid that source in the future (could tell what is that source so we can avoid it too?) – R. Martinho Fernandes Aug 09 '12 at 10:05
  • @R.MartinhoFernandes, looking at the posted code I think etc. has been interpreted as an HTML tag somewhere. – john Aug 09 '12 at 10:09
  • @john that doesn't help in the least. It makes it compile and... do nonsense. – R. Martinho Fernandes Aug 09 '12 at 10:09
  • @R.MartinhoFernandes It might if it's the OP who's made that mistake. – john Aug 09 '12 at 10:10
  • @user: I have tried to improve the question a bit. Feel free to fix if my interpretations of what you wanted was off the mark. – sbi Aug 09 '12 at 10:13

2 Answers2

3

Such tasks are usually easiest done using streams in C++. Something like this should work:

// Beware, brain-compiled code ahead!

#include <vector>
#include <string>
#include <sstream>

std::vector<string> splitClientAuth(const std::string& receiveClient)
{
  // "auth#user:pass"
  std::istringstream iss(receiveClient);

  std::vector<std::string> strings;
  strings.resize(3);
  std::getline(iss, strings[0], '#');
  std::getline(iss, strings[1], ':');
  std::getline(iss, strings[2]); // default is '\n'

  if( !iss && !iss.eof() )
    throw "Dude, you badly need an error handling strategy!";

  if( string[0].empty() || string[1].empty() || string[2].empty() )
    throw "Watcha gonna do now?";

  return strings;
}

A few additional point worth noting:

  • Are those really plain-text passwords?

  • Having this in a std::vector<std::string> seems dubious to me. If that was my code, I'd want a data structure to store user information, and write what I find write into that.

  • Judging from you totally failing to understand the code you pasted in your question (Martinho is right, that's so bad, it's arguable whether it can still be considered C++), and from your comments, you seem to be in bad need of a good C++ book.

Community
  • 1
  • 1
sbi
  • 219,715
  • 46
  • 258
  • 445
2

The code you've found online is garbage. Try this

#include <vector>
#include <string>
using namespace std;

vector<string> split(const string& s, char delim)
{
  vector<string> elems(2);
  string::size_type pos = s.find(delim);
  elems[0] = s.substr(0, pos);
  elems[1] = s.substr(pos + 1);
  return elems;
}

This is untested code, and it doesn't do any error checking (for instance what if s does not contain delim). I'll leave you do sort that out.

You call the function like this

vector<string> x = split(receiveClient, '#');
john
  • 7,897
  • 29
  • 27