![]() |
small doubt in filehandling
hello everyone
please help me with the following code: #include <iostream> #include <fstream> using namespace std; int main() { fstream fin; fin.open("data", ios:: out); string str; cin >> str; fin << str; fin.close(); fin.open("data", ios::in); while(fin) { fin.get(ch); cout << ch; } return 0; } if the input is : priya the output should be priya but my code is showing the output as priyaa please help me to sort out the problem with the above code |
Perhaps you mean this:
Code:
#include <iostream> |
ohh thank you so much . Now its working properly. But will you please say me what is the difference. And also have a look at this code
#include <iostream> #include <fstream> using namespace std; int main() { fstream fin, fin1; fin.open("copy", ios :: out ); int n; string str; cout << "enter no of lines" << endl; cin >> n; int i = 0; do { getline( cin, str ); fin << str << endl; i++; } while ( i < n ); fin.close(); return 0; } In this code i am entering 5 lines but it is taking only 4 lines as input. can u please help me in this case also.what is my wrong here?? |
I'm not sure why that happens but as a workaround, you could flush input with ignore() before reading again:
Code:
#include <iostream> |
thank you so much. My doubt is cleared.
|
| All times are GMT -5. The time now is 10:30 AM. |