files.cpp 717 B

1234567891011121314151617181920212223242526272829303132
  1. // files.cpp -- saving to a file
  2. #include <iostream> // not needed for many systems
  3. #include <fstream>
  4. #include <string>
  5. #include <sstream>
  6. int main()
  7. {
  8. using namespace std;
  9. string filename = "file";
  10. // create output stream object for new file and call it fout
  11. int i;
  12. for (i = 0; i < 140; i++)
  13. {
  14. ostringstream outstr; // manages a string stream
  15. outstr << filename << i;
  16. string fname = outstr.str();
  17. ofstream fout(fname.c_str());
  18. if (!fout.is_open())
  19. break;
  20. fout << "For your eyes only!\n"; // write to file
  21. fout.close(); // close file
  22. fout.clear();
  23. }
  24. cout << "i: " << i << endl;
  25. // std::cin.get();
  26. // std::cin.get();
  27. return 0;
  28. }