c++ - Displaying text from a textfile using strtok_s -


i beginner in c++, trying put text vector textfile using tokens strtok_s.

i can 1 text pushed , displayed in vector , sure there problems in coding.

here codes :

    std::vector<char> myvector;     ifstream myreadfile;     myreadfile.open("e:/c++/projects/textfile project/textfile project/class_data.txt", ios_base::in);      char output[100];     if (myreadfile.is_open()) {         while (!myreadfile.eof()) {             myreadfile >> output;         }      }     myreadfile.close();      char* token = null;     char* context = null;     char delims[] = " ,\t\n";       token = strtok_s(output, delims, &context);      while (token != null)     {         if (token != null)          {             myvector.push_back(token);             token = strtok_s(null, delims, &context);          }     }      (int = 0; < myvector.size(); i++) {         cout << myvector[i] << endl;     } 

i trying text out of text file, separate individual parts , put them vector of chars.

can tell me mistakes have done ? , sorry not professional coding, high school student trying learn c++.

the edited codes, work :

std::vector<std::string> myvector;     ifstream myreadfile;     myreadfile.open("e:/c++/projects/textfile project/textfile project/class_data.txt", ios_base::in);      char output[100];     if (myreadfile.is_open()) {         while (!myreadfile.eof()) {             myreadfile >> output;              char* token = null;             char* context = null;             char delims[] = " ,\t\n";              token = strtok_s(output, delims, &context);              while (token != null)             {                 myvector.push_back(token);                 token = strtok_s(null, delims, &context);             }         }     }     myreadfile.close();      (int = 0; < myvector.size(); i++) {         cout << myvector[i] << endl;     } 

instead of vector of objects of type char std::vector<char> myvector; need define vector of objects of type std::string

std::vector<std::string> myvector; 

this statement

myvector.push_back(token); 

is invalid because trying push pointer instead of character.

also if statement inside while loop

while (token != null) {     if (token != null)      {         myvector.push_back(token);         token = strtok_s(null, delims, &context);      } } 

is superflouos , can removed

while (token != null) {     myvector.push_back(token);     token = strtok_s(null, delims, &context);  } 

also take account in loop

    while (!myreadfile.eof()) {         myreadfile >> output;     } 

object output overwritten. should combine loop loop of splitting output tokens. otherwise dealing last record of file.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -