1

I'm writing a program that takes a txt file like this:

foo.txt:

Aaaa/NGACG/NGAA//
Aaab/AGGGC//
Aaac/CTN/AGGC/NNA//

And in each line it stores the ID (Aaa..) into a vector (once for each value), and the values separated by / into strings.

So the first line would be:

    foo.push_back("Aaaa");
    string bar = NGACG;
    foo.push_back("Aaaa");
    string bar2 = NGAA;

The pseudocode is something like this:

while (not end of file)
{
    while (not end of line)
    {
        while (next char isn't /)
        {
            temporary string x += char
        }
        foo.push_back(string)       //ID

        while (next char isn't /)
        {
            string bar += char      //value
        }
    }
}

My pseudocode is obviously flawed, but that's the general idea of what I want to do. I've looked up guides on how to parse, but nothing really works for my purposes. How can I do this without being completely inefficient? I can't think of how to write this without using an arbitrary number of while loops

moooeeeep
  • 31,622
  • 22
  • 98
  • 187
user3444650
  • 117
  • 9
  • 1
    Have you thought of using something like [boost::split](http://www.boost.org/doc/libs/1_57_0/doc/html/string_algo/usage.html#idp430824992)? – Mohamad Elghawi Oct 02 '15 at 09:17
  • Or just scanf, which accepts some rudimentary pattern as conversions; in a loop, read anything that's not a slash, then read away the slash. Something like this (but with EOF checks): `if( scanf("%[^/]", buf) == 1 ) { strcpy(val, buf); } getchar(); // "/" }` – Peter - Reinstate Monica Oct 02 '15 at 09:36
  • Possible duplicate of [Split a string in C++?](http://stackoverflow.com/questions/236129/split-a-string-in-c) – moooeeeep Oct 02 '15 at 09:59

2 Answers2

1

You can do it the good old C way with fgets and strtok:

char line[SIZE];   // make sure SIZE is greater than your longest input line ...
char *ix;
while(fgets(line), SIZE, fdin) {
    line[strcspn("\n")] = '\0'; // remove EOL
    ix = strtok(line, "/");
    foo.push_back(string(ix));
    string bar = string(strtok(NULL, "/"));
    string bar2 = string(strtok(NULL, "/"));
    ...
}

Or you can use a stringstream and std::getline with the delimiter parameter:

string line;
while(getline(fdin, line)) {
    string temp, bar, bar2;
    istringstream is(line);
    getline(is, temp, '/');
    foo.push_back(temp);
    getline(is, bar);
    getline(is, bar2);
    ...
}

Of course, you should add tests for error conditions...

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
0

You can read the lines (or the whole file) into simple char arrays and use the lines you have read directly: simply replace / with '\0' and point char pointers to the next char into the very lines, without copying anything. The null bytes terminate the "values" between the slashes in the original data and are C strings of their own for all intents and purposes.

Peter - Reinstate Monica
  • 15,048
  • 4
  • 37
  • 62