Parse Split Delimited str.

EDIT::
Here is a literal example of what I need. There will be a CSV file that is formatted like the following:

FName,MInitial,LName,Description,Comment
FName,MInitial,LName,Description,Comment
FName,MInitial,LName,Description,Comment
FName,MInitial,LName,Description,Comment

I need to be able to take those pieces and make something like this:

>> LName >> "." >> FName >> LName >> Comment >> FName >> MInitial >> Description >> endl;
>> LName >> "." >> FName >> LName >> Comment >> FName >> MInitial >> Description >> endl;
>> LName >> "." >> FName >> LName >> Comment >> FName >> MInitial >> Description >> endl;
>> LName >> "." >> FName >> LName >> Comment >> FName >> MInitial >> Description >> endl;


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
#include <string>
#include <vector>
#include <functional>
#include <iostream>
using namespace std;
void split(const string& s, char c,
vector<string>& v) {
string::size_type i = 0;
string::size_type j = s.find(c);
while (j != string::npos) {
v.push_back(s.substr(i, j-i));
i = ++j;
j = s.find(c, j);
if (j == string::npos)
v.push_back(s.substr(i, s.length( )));
}
}
int main( ) {

vector<string> v;
string s = "Account Name|Address 1|Address 2|City";
split(s, '|', v);
for (int i = 0; i < v.size( ); ++i) {
cout << v[i] << endl;
}
getchar();
}


http://www.blog.highub.com/c-plus-plus/c-parse-split-delimited-string/

This code reads through the input and breaks down the string using the delimiter "|".

What I want to do is be able to assign the pieces that it breaks down to temporary strings so I can rearrange them like so:

sample input
val1,val2,val3,val4,val5
val1,val2,val3,val4,val5
val1,val2,val3,val4,val5
val1,val2,val3,val4,val5

sampe output
val3,val4,val5,val1,val2
val3,val4,val5,val1,val2
val3,val4,val5,val1,val2
val3,val4,val5,val1,val2

So, again, what I want it to do is break down the string by reading the data where "val1" would be, assigning that to "temp str1," then "val2" to "tempstr2," 3 and so on until it completes val5. Then it does something like

outputfile >> str3 >> str4 >> str5 >> str1 >> str2 >> endl;

and just loop until doc!=good or something.

Any ideas?
Last edited on
I don't really understand what you want, so I can advise something like this:
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
#include <string>
#include <vector>
#include <functional>
#include <iostream>
#include <algorithm>
using namespace std;
void split(const string& s, char c,
vector<string>& v) {
string::size_type i = 0;
string::size_type j = s.find(c);
while (j != string::npos) {
v.push_back(s.substr(i, j-i));
i = ++j;
j = s.find(c, j);
if (j == string::npos)
v.push_back(s.substr(i, s.length( )));
}
}
int main( ) {

vector<string> v;
string s = "Account Name|Address 1|Address 2|City";
split(s, '|', v);
for (int i = 0; i < v.size( ); ++i) {
rotate(v.begin(), v.begin() + 2, v.end());
cout << v[i] << endl;
}
getchar();
}
Thanks for your post! That actually does what I stated I needed done, but I didn't say very well what I needed.

Here is a literal example of what I need. There will be a CSV file that is formatted like the following:

FName,MInitial,LName,Description,Comment

I need to be able to take those pieces and make something like this:

>> LName >> "." >> FName >> LName >> Comment >> FName >> MInitial >> Description >> endl;

Does that make more sense?
Updated with restated question.
closed account (zb0S216C)
What you need to do, my friend, is tokenize your string. You can do this via strtok( )[1]. What strtok( ) is supposed to do is split a given string into tokens. Each token determined by the given delimiter (s).

References:
[1]http://www.cplusplus.com/reference/clibrary/cstring/strtok/


Wazzak
Last edited on
But I'm still at a loss to figure out how to be able to assign the values to more than one string in order to rearrange them though.

This is a little update: Updated# n

This is what I am playing around with now:

I added char "*str2 - *str7" while I was playing with it.

Is there a way I can adapt this to possibly take that string "this is a test of string tokenizing" and break each one up (using a delimiter of course) to be able to write my own order to a file? So, say in a perfect world:

str1= this
str2= is
str3= a
str4= test
str5= of
str6= string
str7= tokenizing

so I could do something like:

outputfile >> str4 >> str6 >> str3 >> str2 >> str1 >> str7 >> str5 >> str3 >> str3 >> endl;


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
#include stdio.h
#include string.h
 
int main(int argc, char *argv[])
{
        int x = 1;
        char str[]="this:is:a:test:of:string:tokenizing";
        char *str1;
		char *str2;
		char *str3;
		char *str4;
		char *str5;
		char *str6;
		char *str7;
 
        /* print what we have so far */
        printf("String: %s\n", str);
 
        /* extract first string from string sequence */
        str1 = strtok(str, ":");
 
        /* print first string after tokenized */
        printf("%i: %s\n", x, str1);
 
        /* loop until finishied */
        while (str1!=NULL)
        {
                /* extract string from string sequence */
                str1 = strtok(NULL, ":");
 
                /* check if there is nothing else to extract */
                if (str1 == NULL)
                {
                        printf("Tokenizing complete\n");
				}
 
                /* print string after tokenized */
                printf("%i: %s\n", x, str1);
                x++;
        }
		 getchar();
        return 0;
 
}
closed account (DSLq5Di1)
enum csv { AccName, Addr1, Addr2, City };

Using the code from your first post,
1
2
3
4
5
6
7
int main( ) {

    vector<string> v;
    string s = "Account Name|Address 1|Address 2|City";
    split(s, '|', v);

    cout << v[AccName] << v[Addr1] << v[Addr2] << v[City] << endl;

Is that what you wanted?
Yeah, kind of. I wanted to be able to repeat the variables as well. I have the finished code that works for me.

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
// reading a text file
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main () {
  string line;
  ifstream infile ("example.csv");
  ofstream outfile ("testing123123.txt");
if (infile.is_open())
    {
        while (getline(infile, line))
        {
            // copy the string to a character array
            char* pszLine = new char[line.length() + 1];
            strcpy(pszLine, line.c_str());
            char* szToken[5];
 
            // split the line into tokens
            szToken[0] = strtok(pszLine, ",");
            for (int i = 1; i < 5; ++i)
            {
                szToken[i] = strtok(NULL, ",");
            }
 
            // write the tokens out in a different order
            outfile <<  szToken[2] << " ";
            outfile <<  szToken[3] << " ";
            outfile <<  szToken[0] << " ";
            outfile <<  szToken[1] << " ";
            outfile <<  szToken[4] << " ";
            outfile <<  szToken[0] << ".";
            outfile <<  szToken[2] << endl;
 
            // tidy up
            delete []pszLine;
        }
}

}
Topic archived. No new replies allowed.