I am trying to read two strings per line and store them in strings so i can use them in my program.for example if this is my input file:


i wan to store cat as animal1 string dog as animal2 string and yellow as colour1 string and so on.not sure how to do this.What i got so far is reading line by line which is not what i want.

#include <fstream>
#include <string>
using namespace std;

int main () {
  string line;
  //the variable of type ifstream:
  ifstream myfile ("example.txt");
  //check to see if the file is opened:
  if (myfile.is_open())
    //while there are still lines in the
    //file, keep reading:
    while (! myfile.eof() )
      //place the line from myfile into the
      //line variable:
      getline (myfile,line);
      //display the line we gathered:
      cout << line << endl; 
    //close the stream:
  else cout << "Unable to open file";

    return 0;

would be great if someone help me with this,thank

Look again at getline() and consider its 3rd parameter.

i tried getline (myfile,line,":");
but didnt work.

i tried getline (myfile,line,":");
but didnt work.

My guess is you did it wrong. But since we can't see your screen, it's hard to suggest a fix.

Hmmm I did something similar, except i stored the strings from the file into a vector. What i did was:


1 being the filename
2 being the variable used to store the value in
and the 3rd would be the divider which signals the code to stop reading.

Let's say you have this line in your input file:


and your code goes like this getline(myfile,input,"/");
it would be read as animal1 and color1 respectively

Then again I'm just making assumptions here, gotta check out your input file as well