1
0

fileio.cpp 987 B

1234567891011121314151617181920212223242526272829303132333435
  1. // fileio.cpp -- saving to a file
  2. #include <iostream> // not needed for many systems
  3. #include <fstream>
  4. #include <string>
  5. int main()
  6. {
  7. using namespace std;
  8. string filename;
  9. cout << "Enter name for new file: ";
  10. cin >> filename;
  11. // create output stream object for new file and call it fout
  12. ofstream fout(filename.c_str());
  13. fout << "For your eyes only!\n"; // write to file
  14. cout << "Enter your secret number: "; // write to screen
  15. float secret;
  16. cin >> secret;
  17. fout << "Your secret number is " << secret << endl;
  18. fout.close(); // close file
  19. // create input stream object for new file and call it fin
  20. ifstream fin(filename.c_str());
  21. cout << "Here are the contents of " << filename << ":\n";
  22. char ch;
  23. while (fin.get(ch)) // read character from file and
  24. cout << ch; // write it to screen
  25. cout << "Done\n";
  26. fin.close();
  27. // std::cin.get();
  28. // std::cin.get();
  29. return 0;
  30. }