C++ fstream Stores the alphabet in a file, reads two letters from it
#include <fstream.h> #include <stdlib.h> #include <stdio.h> fstream fp;/* w ww. ja va 2 s .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, SEEK_SET); // Skip eight letters, point to I. fp >> ch; // Change the Q to an x. fp.seekg(-1L, SEEK_CUR); fp << 'x'; cout << "The first character is " << ch << "\n"; fp.seekg(16L, SEEK_SET); // Skip 16 letters, point to Q. fp >> ch; cout << "The second character is " << ch << "\n"; // Change the Q to an x. fp.seekg(-1L, SEEK_CUR); fp << 'x'; fp.close(); return; }