Advertisement
andruhovski

sp0205

Oct 23rd, 2014
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 KB | None | 0 0
  1. // fstream_demo.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <fstream>
  7. using namespace std;
  8.  
  9. int demo01();
  10. int demo02();
  11. int demo03();
  12.  
  13. int _tmain(int argc, _TCHAR* argv[])
  14. {
  15.     demo01();
  16.     demo02();
  17.     demo03();
  18.     return 0;
  19. }
  20.  
  21.  
  22. int demo01() {
  23.     ofstream myfile;
  24.     myfile.open("example.txt");
  25.     myfile << "Writing this to a file.\n";
  26.     myfile.close();
  27.     return 0;
  28. }
  29.  
  30.  
  31. int demo02() {
  32.     ofstream myfile("example.txt");
  33.     if (myfile.is_open())
  34.     {
  35.         myfile << "This is a line.\n";
  36.         myfile << "This is another line.\n";
  37.         myfile.close();
  38.     }
  39.     else cout << "Unable to open file";
  40.     return 0;
  41. }
  42.  
  43. int demo03() {
  44.     streampos size;
  45.     char * memblock;
  46.  
  47.     ifstream file("example.bin", ios::in | ios::binary | ios::ate);
  48.     if (file.is_open())
  49.     {
  50.         size = file.tellg();
  51.         memblock = new char[static_cast<int>(size)];
  52.         file.seekg(0, ios::beg);
  53.         file.read(memblock, size);
  54.         file.close();
  55.  
  56.         cout << "the entire file content is in memory";
  57.  
  58.         delete[] memblock;
  59.     }
  60.     else cout << "Unable to open file";
  61.     return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement