Quantcast
Channel: CodeChef Discuss - latest questions
Viewing all articles
Browse latest Browse all 39796

C++ File Handling Issue

$
0
0

Hello Guys, I was trying file handling with C++. The problem that I encountered was that when I read from a file using << operator the last string was printed twice.

ifstream in("Sample.txt"); if(!in) { cout<<"Unable to open file\n"; return 1; } string str; / issue occurs here / while(in){ in >> str; cout << str << endl; } in.close();

Suppose 'Sample.txt' file contains the string "Hello World" Then World is being printed twice. Now when I declare 'string str' inside the while loop it is giving correct o/p. So what could be the problem here?

I think that when the EOF is being reached it is not updating the string that is why when string is globally declared it is printing the old value. Kindly Help!


Viewing all articles
Browse latest Browse all 39796

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>