Tricky Tricks ›› Programming Tricks ›› C Languages Tricks ›› When Reading An Entire File, Embed The File Input Inside Of The Loop Condition

When Reading An Entire File, Embed The File Input Inside Of The Loop Condition

C Programming Tricks

When reading an entire file, embed the file input inside of the loop condition:

 std::ifstream inf("data.txt");
 char temp[100];
 while(!inf.getline(temp, 100).eof())
 {
   //process the line
 }
 
 the loop will exit once the end of the file is reached
 

Partners