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.
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.