-1

I'm trying to read from a textfile and then trying to place those in an object.

But I am not sure how to seperate a line into two variables (space as a delimiter).

The text file format is as follows (first 2 entries):

    Text1 [txt1]
    This is a description of text 1.More description of Text 1 here blah       blah blah.

    Text2 [txt2]
    This is the description of text 2.

I would like to place them into three sperate variables, one for the name (Text1), type which are the square brackets ([txt1) and finally the description (This is a description of text 1.)

I have the following code so far which does not seperate Text1 and [txt1]:

    if (myfile.is_open()) {
    string buffer;
    string currentText= "empty";
    string currentType= "empty";
    string currentDescription = "empty";

    while (!myfile.eof()) {

        getline(myfile, buffer); // Would like to seprate this line into two variables
        currentText = buffer;
        if (buffer == "") continue;

        getline(myfile, buffer);
        currentDescription = buffer;
        if (buffer == "") continue;

        TextObj newtext(currentText, iWantToEnterTypeHere, currentDescription);
        this->textVector.push_back(newText);
    }
    myfile.close();

I hope this made sense, I would appreciate any help. Cheers

Berimbolo
  • 53
  • 1
  • 1
  • 3
  • 1
    For why `while (!myfile.eof()) {` is wrong, see https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong –  Apr 05 '18 at 00:41
  • [`getline`](http://www.cplusplus.com/reference/string/string/getline/) has another signature that takes a third paramter--a char-- that it uses as a delimiter. By default when you use the two parameter version, that delimiter is the newline character. There's probably a dupe somewhere... – scohe001 Apr 05 '18 at 00:41
  • Why not build on top of `ifstream`? Instead of storing it as a string? –  Apr 05 '18 at 00:53
  • @GRC, Can you please elaborate? – Berimbolo Apr 05 '18 at 01:28
  • @NeilButterworth, thank you, very interesting. – Berimbolo Apr 05 '18 at 01:29
  • I will post my answer in about 20 minutes. –  Apr 05 '18 at 01:29

1 Answers1

0
#include <iostream>
#include <vector>
#include <sstream>
#include <fstream>

using namespace std;

int main() {
    fstream myfile;
    myfile.open ("example.txt");
    if (myfile.is_open()) {
        string line;
        while (getline (myfile,line)) {
        vector<string> tokens;
        const string s =  line;
        char delim =' ';
        stringstream ss(s);
        string item;
        while (getline(ss, item, delim)) {
            tokens.push_back(item);
        }   
        getline (myfile,line);
        tokens.push_back(line);
        //Here you can create object using token[0], token[1], token[2] and can remove cout statements
        for(int i = 0; i < tokens.size(); i++) {
                cout << tokens[i] << endl;
        } 
        cout<<"=================\n";
     }
    }
    myfile.close();
    return 1;
}

I hope this helps. You can create the object as specified in the comments block and remove cout statements. They were added just to add clarification.