fork download
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. int main()
  5. {
  6. fstream file;
  7. file.open("sample.txt",ios::out);
  8. if(!file)
  9. {
  10. cout<<"Error in creating file!!!";
  11. return 0;
  12. }
  13. file<<"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  14. cout<<"Current position is: "<<file.tellp()<<endl;
  15. file.close();
  16. file.open("sample.txt",ios::in);
  17. if(!file)
  18. {
  19. cout<<"Error in opening file!!!";
  20. return 0;
  21. }
  22. cout<<"After opening file position is: "<<file.tellg()<<endl;
  23.  
  24. char ch;
  25. while(!file.eof())
  26. {
  27. cout<<"At position : "<<file.tellg();
  28. file>>ch;
  29. cout<<" Character \""<<ch<<"\""<<endl;
  30. }
  31. file.close();
  32. return 0;
  33. }
  34.  
  35.  
  36.  
Success #stdin #stdout 0s 4552KB
stdin
Standard input is empty
stdout
Error in creating file!!!