Help regarding file handling. (email header analyzer program)

I'm doing a c++ program to display email header from a file "input.txt" where header is stored.I want to show From, To, Date and Subject. I searched on google and I found the following program program.I've understood most of the program but I didn't understand few lines. I want to know why they have taken string array -
string temp1[2]; //line number 11
Also I didn't understand following statement:
while(file>>temp1[i]) //line number 15
What is the use of >> operator here?
Also why didn't they increment i ?
Please help me understand this. Thank You.

PROGRAM:

#include <iostream>
#include"fstream"
using namespace std;

int main()
{
ifstream file("input.txt",ios::in);

string from, to, subject, date;

string temp1[2];

int i=0;

while(file>>temp1[i])
{
if(temp1[i].compare("Date:")==0)
getline(file,date);
else if(temp1[i].compare("Subject:")==0)
getline(file,subject);
else if(temp1[i].compare("From:")==0)
getline(file,from);
else if(temp1[i].compare("To:")==0)
getline(file,to);
}

cout<<"\nFrom:\t"<<from<<endl;
cout<<"\nTo:\t"<<to<<endl;
cout<<"\nSubject:\t"<<subject<<endl;
cout<<"\nDate:\t"<<date<<endl;

cout<<endl;

return 0;
}
why they have taken string array
Looks line no reason at all. Maybe early decigion which got reworked later. A single string would work too.

Also I didn't understand following statement:
while(file>>temp1[i]) //line number 15
What is the use of >> operator here?
Extraction from stream, like cin >> something. That operator returns reference to stream operated upon, so you could chain calls. Another feature of streams is that you can convert them to boolean value to check if error occured in previous I/O operation. So this line whould be read as "while we succesfully read next string from file".
Also why didn't they increment i ?
There is no need. A single string instead of array would work just as fine: we only use it as temporary storage.
Topic archived. No new replies allowed.