slicing a string

hello everybody
i want to know how can i split a string in smaller strings. yes, i do know about substr(). but the problem is how to find exactly where it should be sliced.

so far, i have a extern text file with the following:
[Weapon]
ID Name Value Weight Attack
#10001 <Stick> <80> <8> <1>
#10002 <Claw> <100> <10> <2>
#10003 <Sword> <150> <10> <3>
[...]


and the code is like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int id = 10002;
std::ifstream file("data/text");
if (file.is_open()){
    std::string id_str = "#" + std::to_string(id);
    std::string srch_line;
    std::cout << "ID: " << id_str << std::endl;
    while (file.good()){
        std::getline (file, srch_line);
        if (srch_line.find(id_str) != std::string::npos){
            std::cout << srch_line << std::endl;
            //STUCK HERE

        }
    }
}


so what it do is: load the text file, find the 'id' string inside it, copy the correspondent whole line to a string named srch_line, and output the line (just to make sure it is correct).
what i want to do is get the text inside the first < and > signals, and then the text inside the next ones, and next, etc... and assign each one to a different string.

thanks in advance!
you can use std::istringstream:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <sstream>

int main()
{
    std::string srch_line = "#10002 <Claw> <100> <10> <2>";
    std::istringstream iss(srch_line);
    std::string s1, s2, s3, s4;
    
    getline(iss, s1, '<');
    getline(iss, s1, '>');
    
    getline(iss, s2, '<');
    getline(iss, s2, '>');
    
    getline(iss, s3, '<');
    getline(iss, s3, '>');
    
    getline(iss, s4, '<');
    getline(iss, s4, '>');
    
    std::cout << s1 << "\n"
              << s2 << "\n"
              << s3 << "\n"
              << s4 << "\n";
}
thanks for the answer! but i really didn't get it.
why don't everything just become "Claw"? the same command is just repeated for all the strings (s1, s2, etc) :o
http://www.cplusplus.com/reference/string/string/getline/

At the beginning, s1 stores nothing, iss has "#10002 <Claw> <100> <10> <2>"

>>>getline(iss, s1, '<');
Extracts characters from iss and stores them into s1 until the delimitation character '<' is found. The delimiter '<' is extracted and discarded from iss, too.

s1 now stores "#10002 "

iss has "Claw> <100> <10> <2>"



>>>getline(iss, s1, '>');

s1 now stores "Claw"

iss has " <100> <10> <2>"



>>>getline(iss, s2, '<');

s2 now stores " "

iss has "100> <10> <2>"



>>>getline(iss, s2, '>');

s1 now stores "100"

iss has " <10> <2>"


...
Last edited on
You want to do this include the sstream header

1
2
3
4
5
6
ifstream file("file.txt");
string line, id, name, value,  weight, attack; //strings to hold line and values
while(getline(file,line){ //while it can read a line from your file
         istringstream ss(line); //assocciate the line with the string stream
         ss >> id  >> name >> value >> weight >> attack; // read each value into the string
//store the data or do something with it} 


if you want to get rid of the <> you can use:

1
2
attack.erase(attack.begin());
attack.erase((attack.end() - 1));


Last edited on
ooh, now i got it. i didn't knew that getline changes the original string. thanks for the help :)
thanks also, pata.
Last edited on
Topic archived. No new replies allowed.