i have two questions:
1) why does the line cout<<line<<endl; in the end of the file doesnt print anything on the screen?
2)lets say i want the string line to hold only, for example, the second word in the file, how can i do that??
If those words would have been seperated in "paragraphs" like
"word1
word2
word3"
you could use something like while (getline(myfile, firstword) && getline(myfile, secondword))
1) why does the line cout<<line<<endl; in the end of the file doesnt print anything on the screen?
Because the original code reads the first line then immediately closes the file. Then the loop is executed a second time (which doesn't make much sense, as the file is now closed), but line is set to an empty string, and both the fail() and eof() flags are set for the file, thus terminating the loop.
If you wanted to output the second line from the file (which I know wasn't exactly what you asked), you might do something like this: