function outputs to screen. I need output to array

I asked a question earlier on here and got some code. This code works perfect for me except its outputs path,file to screen and I need the output in an array and not to the screen.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 //string::find_last_of
#include <iostream>       // std::cout
#include <string>         // std::string
#cinlude <cstddef>        // std::size_t

void SplitFilename (const std::string& str)
{
  std::cout << "Splitting: " << str << '\n';
  std::size_t found = str.find_last_of("/\\");
  std::cout << " path: " << str.substr(0,found) << '\n';
  std::cout << " file: " << str.substr(found+1) << '\n';
}

int main ()
{
  std::string str1 ("/usr/bin/man");
  std::string str2 ("c:\\windows\\winhelp.exe");

  SplitFilename (str1);
  SplitFilename (str2);

  return 0;
}
Do you realize that the std::string class has quite a few member functions? Perhaps you should study the documentation for this important C++ class: http://www.cplusplus.com/reference/string/string/

Also be careful with the find function. Pay very close attention to the return value, especially when the find fails.
Last night I was looking to rewrite this example to work for me. I came up with this and its not in a function and would prefer it to be that way but its working so problem solved for now.

1
2
3
4
std::size_t found = copyArray[0].find_last_of("/");
std::size_t found1 = copyArray[1].find_last_of("/");
output1 = copyArray[0].substr(found+1); //File
output2 = copyArray[1].substr(found1+1); //Path 


This worked for me.
Last edited on
Topic archived. No new replies allowed.