This was an example in my book on put and get method for file. It doesn't compile. And now I am confused. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

This was an example in my book on put and get method for file. It doesn't compile. And now I am confused.

#include<iostream> #include<conio.h> #include<cstdlib> #include<fstream> #include<cstring> using namespace std; int main() { char string[20]; cout<<"Enter a String: "; cin>>string; int len=strlen(string); fstream file; cout<<"Opening the TEXT file \n\n"; file.open("TEXT", ios::in|ios::out); for(int i=0; i<len; i++) file.put(string[i]); file.seekg(0); char ch; cout<<"Reading the file Contents: "; while (file) { file.get(ch); cout<<ch; } return 0; }

19th Jun 2020, 7:11 AM
Prashant Pant
Prashant Pant - avatar
1 Answer
0
My initial, superficial test in SoloLearn doesn't protest on compilation errors. Can you please be more specific in the nature of the error you're getting? Mind you, if you try to run this code in the platform, it won't end at all (because of the file handling). Using a stand-alone compiler in a desktop computer should work, though.
19th Jun 2020, 7:54 AM
Felipe BF