Grabbing words from a text

Hello guys, I have been searching everywhere here and i would appreciate if i could get some help on starting this program.

What im trying to do is get a huge text file, grab information from it and output into another file, for example the file contains:

userid = xxsfdfafa, name = vinnidrk, product = 12341,
otherfield = 1
otherfield2 = 2
otherfield3 = 4

userid = asdsada, name = anotherperson, product = 424123,
otherfield = 3
otherfield2 = 5
otherfield3 = 1

What i want, is to be able to get the name for every account until end of file and output it one under the other.
vinnidrk
anotherperson

Thanks guys


Something along these lines:
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <string>
#include <sstream>

void copy_names( std::istream& input, std::ostream& output )
{
    static const std::string name_tag = "name = " ;
    static const auto name_tag_sz = name_tag.size() ;
    static const std::string product_tag = ", product =" ;

    std::string line ;
    while( std::getline( input, line ) )
    {
        auto begin = line.find(name_tag) ; // try to find name_tag
        if( begin != std::string::npos ) // if found
        {
            begin += name_tag_sz ; // set begin of name to char just after name_tag

            const auto end = line.find( product_tag, begin  ) ; // try to find product_tag
            // if found, name is everything up to the product_tag,
            // else everything up to end of line
            auto len = end != std::string::npos ? end-begin : end ;

            output << line.substr( begin, len ) << '\n' ; // extract ant write the name
        }
    }
}

int main()
{
    std::istringstream file( R"(
                                    userid = xxsfdfafa, name = vinnidrk, product = 12341,
                                    otherfield = 1
                                    otherfield2 = 2
                                    otherfield3 = 4

                                    userid = asdsada, name = anotherperson, product = 424123,
                                    otherfield = 3
                                    otherfield2 = 5
                                    otherfield3 = 1

                                    userid = vhhvjv, name = a third person, product = 999,
                                    otherfield = 8
                                    otherfield2 = 3
                                    otherfield3 = 4
                               )" ) ;
    copy_names( file, std::cout ) ;
}

http://coliru.stacked-crooked.com/a/90ab9f35494b11a4
thank you for the reply but im getting compile errors on the name_tag_sz and this is getting information from a file, not that its written in the program like, example.txt
> im getting compile errors on the name_tag_sz

Type inference with auto requires C++11.
For legacy C++, modify to:
1
2
3
4
5
6
7
8
// static const auto name_tag_sz = name_tag.size() ;
static const std::size_t name_tag_sz = name_tag.size() ;
...
//const auto end = line.find( product_tag, begin  ) ;
const std::size_t end = line.find( product_tag, begin  ) ;
...
// auto len = end != std::string::npos ? end-begin : end ;
std::size_t len = end != std::string::npos ? end-begin : end ;



> this is getting information from a file, not that its written in the program like, example.txt

So, write your main() to read from and write to files.
1
2
3
4
5
6
int main()
{
    std::ifstream input_file( "my_input_file.txt" ) ;
    std::ofstream output_file( "my_output_file.txt" ) ;
    copy_names( input_file, output_file ) ;
}

how do i translate the following line?

auto begin = line.find(name_tag) ; // try to find name_tag

to legacy c++?
Topic archived. No new replies allowed.