read a text file problem

Dear friend I want read a text file in c++ and than I want write to this file on the screan but I make some mistakes please help me..
This is text files content:
Emek 87.sok 34 ankara
Firstly my program should be read this info and than write on the screen like this:
******
adress
******
semt: Emek
sokak: 87.sok
bina: 34
il: ankara

THIS IS MY PROGRAM
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
string semt,sokak,bina,il;
ifstream myfile ("adress.txt");
if (myfile.is_open())
{
while (! myfile.eof() )
{
//? how can I write here???
cout <<"semt: "<< semt << endl;
cout <<"sokak: "<< sokak << endl;
cout <<"bina: "<< bina << endl;
cout <<"il: "<< il << endl;
}

myfile.close();
}

else cout << "Unable to open file";
system("pause");
return 0;
}

Instead of this: while (! myfile.eof() ) you could have this: while (myfile >> semt >> sokak >> bina >> il ). That actually reads the values from your input file and will only continue if the read was successful.
thanks so much Galik for your help I solve my problem with your helping..
I'm surprised that this code works. Does it mean that calling ifstream::open() is not mandatory?
If you pass the file name to the constructor it will open the file, so you don't need to call open().

So these are equivalent:
1
2
3
4
std::ifstream ifs1("input.txt");

std::ifstream ifs2;
ifs2.open("input.txt");
Just to add to what Galik said, you also don't need to call close(), you can just let the stream go out of scope and the destructor will close the file for you.
Topic archived. No new replies allowed.