0

Good evening everyone,

I'm trying to read from a text file source with the following format:

   doomsday [villain]
   Doomsday is a fictional supervillain appearing in American comic books published by DC Comics.

   superman [hero]
   Superman is a fictional superhero appearing in American comic books published by DC Comics.

These are two entries. I need to extract the name, role and description so I can place it into an object e.g.

   DCCharacters newCharacter(characterName, characterRole, description);

My code so far:

ifstream fileToOpen(fileSrc);

if (fileToOpen.is_open()) {
    string line;
    string charName;
    string charRole;
    string charDesc;

    while (!fileToOpen.eof()) {

        getline(fileToOpen, line);
        charName = line;
        if (line == "") continue; // Skip blank lines

        getline(fileToOpen, line);
        charDescription = line;
        if (line == "") continue;

        DCCharacters newCharacter(characterName, characterRole, description);
        this->DCCharsVector.push_back(newCharacter);
    }
    // Close the file
     fileToOpen.close();
     }
 }

As you can see I can extract the character name and role in one line and the description in another, but I dont now how to seperate the name and role into two seperate variables to put them into the object like I need to.

I'm looking forward to any help.

  • Possible duplicate of [Split string by single spaces](https://stackoverflow.com/questions/5888022/split-string-by-single-spaces) – moooeeeep Apr 11 '18 at 09:38
  • You should check the return code of `getline()` before using the contents of `line`. You should consider to implement the `operator>>()` overloads for your class... – moooeeeep Apr 11 '18 at 09:40

1 Answers1

0

std::getline() has one more overload, which should solve your problem:

istream& getline (istream&  is, string& str, char delim);

If you use ' ' (space character) as delimiter, std::getline() will read only up to first space it sees.

So, your code could look like this:

if (fileToOpen.is_open()) 
{
    string line;
    string charName;
    string charRole;
    string charDesc;

    while (!fileToOpen.eof()) 
    {
        getline(fileToOpen, line, ' ');
        charName = line;
        if (line == "") continue; // Skip blank lines

        getline(fileToOpen, line);
        charRole = line; //possibly strip off '[]' before assigning
        if (line == "") continue; // Skip blank lines

        getline(fileToOpen, line);
        charDescription = line;
        if (line == "") continue;

        DCCharacters newCharacter(characterName, characterRole, description);
        DCCharsVector.push_back(newCharacter);
    }
// Close the file
    fileToOpen.close();
}

On a side note, you needlessly copy strings lots of times. You could pass charName etc. as argument to std::getline() and get rid of line variable.
Also, since you tagged C++11, you could use

DCCharsVector.emplace_back(characterName, characterRole, description) 

to avoid creating temporary object.

Yksisarvinen
  • 18,008
  • 2
  • 24
  • 52