C++ fstream and printing to terminal -


this question has answer here:

i've got bit of code trying work. want open file , print contents terminal. right i've got list (1-10) in .txt file in same folder .cpp file.

int main() {   ifstream infile;       infile.open("numbers.txt");    if( infile.fail()) {         cout<<"error opening file "<< endl;         return 0;   }    while(!(infile.fail())) {     int x;     infile >> x;     cout<<x<< endl;   } } 

this have far , works open file , print console. issue is, prints last line of file twice ( print 1-10 fine prints 10 twice) i've stumped myself trying figure out. ideas?

thanks helping me edit this!

try below code

#include <iostream> #include <fstream> using namespace std; int main() {   ifstream infile;   infile.open("a.txt");    if( infile.fail()) {     cout<<"error opening file "<< endl;     return 0;   }    int x;   while(infile >> x) {     cout<<x<< endl;   } } 

Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -