C++ examples for File Stream:Text File
Stores the alphabet in a file, then reads two letters from it.
#include <fstream> #include <iostream> using namespace std; #include <stdlib.h> #include <stdio.h> fstream fp;/* ww w.j av a 2s .c om*/ void main() { char ch; // Holds A through Z. // Open in update mode so you can read file after writing to it. fp.open("alph.txt", ios::in | ios::out); if (!fp) { cout << "\n*** Error opening file ***\n"; exit(0); } for (ch = 'A'; ch <= 'Z'; ch++) { fp << ch; } // Write letters. fp.seekg(8L, ios::beg); // Skip eight letters, point to I. fp >> ch; cout << "The first character is " << ch << "\n"; fp.seekg(16L, ios::beg); // Skip 16 letters, point to Q. fp >> ch; cout << "The second character is " << ch << "\n"; fp.close(); return; }