0

I am using Visual Studio 2010 C++ and I have a long string with multiple paths like this.

C:\eula0.txt

C:\eula1.txt

C:\eula2.txt

C:\eula3.txt

C:\eula4.txt

all above file paths are in a single string "S". There is a new line char "\n" between each path. I want to extract each path as a single string path.

The final output should be like this.

string s0 = C:\eula0.txt

string s1 = C:\eula1.txt

string s2 = C:\eula2.txt

string s3 = C:\eula3.txt

string s4 = C:\eula4.txt

How can I do this. Please help me. Thanks.

maxpayne
  • 1,111
  • 2
  • 21
  • 41

6 Answers6

2

Try getline:

#include <string>
#include <sstream>

std::string S = /* your string */;
std::istringstream iss(S);

for (std::string line; std::getline(iss, line); )
{
    std::cout << "Have file: " << line << "\n";
}
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
2

This example copies the individual strings into a vector and prints out each one of them to stdout. It relies on the fact that the istream_iterator uses \n as a separator. Beware, it will also use other whitespace character as separators, so it would not work if your file names contain whitespace.

#include <string>
#include <iostream>
#include <sstream>
#include <iterator>
#include <vector> 
int main()
{
  std::string s("C:\\eula0.txt\nC:\\eula1.txt\nC:\\eula2.txt");

  std::stringstream str(s);
  std::vector<std::string> v((std::istream_iterator<std::string>(str)),
                             (std::istream_iterator<std::string>()));

  for (const auto& i : v) 
    std::cout << i << "\n";
}
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
1

Just create your own custom a loop with std::string's find_first_of member function. That would be one way. There are many other ways.

user2015453
  • 4,844
  • 5
  • 25
  • 27
1

You can use istringstream and getline for this

std::istringstream ss(S);
std::string s0, s1, s2, ...;
std::getline(ss, s0);
std::getline(ss, s1);
...
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
1

Another way, or more C way:

char *tmp;
char *input = ...; // Pointer to your input, must be modifiable null terminated string
char *buffer[256]; // Pick appropriate size
int i=0;

while((tmp=strchr(input, '\n'))
{
    *tmp = 0;
    buffer[i++] = strdup(input);
    input = ++tmp;
    // We replaced the newlines with null-terminators,
    // you may now undo that if you want.
}
buffer[i] = strdup(input); // Last entry or if no newline is present

P.s Don't forget later on to free the memory strdup allocates for you and do some sanity checkings :)

(Tell me if you need me to tell whats going on here, and I'll explain it further.)

Jite
  • 4,250
  • 1
  • 17
  • 18
0

I wrote in this way. Working perfect.

Thanks to all.

void GetFilePathFromMultipleStringPaths(const char* urls)
{
    int n = 0;
    vector<string> tokens; // Create vector to hold paths
    std::string s = urls;
    std::string::size_type prev_pos = 0, pos = 0;
    while( (pos = s.find('\n', pos)) != std::string::npos )
    {
        std::string substring( s.substr(prev_pos, pos-prev_pos) );
        tokens.push_back(substring);
        prev_pos = ++pos;
        n++;
    }
    std::string substring( s.substr(prev_pos, pos-prev_pos) ); // Last path
    if(substring.data() != NULL)
    {
        tokens.push_back(substring);
        n++;
    }
    for (int i=0; i<n; i++)
    {
        cout<<tokens[i].c_str()<<"  "<<i<<endl;
    }
}
maxpayne
  • 1,111
  • 2
  • 21
  • 41