Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // fstream_demo.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- #include <fstream>
- using namespace std;
- int demo01();
- int demo02();
- int demo03();
- int _tmain(int argc, _TCHAR* argv[])
- {
- demo01();
- demo02();
- demo03();
- return 0;
- }
- int demo01() {
- ofstream myfile;
- myfile.open("example.txt");
- myfile << "Writing this to a file.\n";
- myfile.close();
- return 0;
- }
- int demo02() {
- ofstream myfile("example.txt");
- if (myfile.is_open())
- {
- myfile << "This is a line.\n";
- myfile << "This is another line.\n";
- myfile.close();
- }
- else cout << "Unable to open file";
- return 0;
- }
- int demo03() {
- streampos size;
- char * memblock;
- ifstream file("example.bin", ios::in | ios::binary | ios::ate);
- if (file.is_open())
- {
- size = file.tellg();
- memblock = new char[static_cast<int>(size)];
- file.seekg(0, ios::beg);
- file.read(memblock, size);
- file.close();
- cout << "the entire file content is in memory";
- delete[] memblock;
- }
- else cout << "Unable to open file";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement