Hello,I need to read a file containing 161 commands and store it in a vector of strings using vector_pushback and then print the 161 commands.Which is the best way to proceed with this ?

Recommended Answers

All 2 Replies

uset getline to get you file a line at a time as a string and then vector::push_back to add it to the vector. The will be a little inefficiency as the vector reallocates memory as it grows but for 161 entries this is unlikely to be a big issue.

How are the commands stored?

One approach is to do something of the following:

struct Command{
   /* whatever makes a command, insert variables here */
   friend ostream& operator>>(ostream&, Command& cmd){ 
      //given a stream, try to read in a command
   }
 }

 int main(){
   //open file and stuff
   std::vector<Command> commands;
   Command tmpCmd;
   //read until failure
   while( fileInput >> tmpCmd) commands.push_back(tmpCmd);

  //print and stuff

  return 0;
 }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.