// Program to manipulate a text file character by character. // Assignment-32 // fchrbchr.cpp, Author - Krishna Kumar Khatri. // Uncomment ONLY the following line if you are using Turbo/Borland compiler. // #define TC #ifdef TC #include #include #else #include #include #include using namespace std; #endif #include // For getch. int main() { #ifdef TC clrscr(); #endif const int MAXLEN = 80; fstream fp; char* fn = new char[MAXLEN / 4]; char* contents = new char[MAXLEN * 2]; int i, contlen; cout << "Enter the name for file to be created: "; cin >> fn; cout << endl << "Use tilde '~' character for string termination."; cout << endl << endl << "Enter the contents: "; cin.getline(contents, (MAXLEN * 2), '~'); fp.open(fn, ios::in | ios::out | ios::trunc); // Open the file for both operations. contlen = strlen(contents); cout << endl << "Writing -> "; for (i = 1; i < contlen; i++) { // Starting from 1 to avoid first char cout << contents[i] << " "; // repetition. Strange! fp.put(contents[i]); } cout << endl << "File was written successfully."; fp.seekg(0); // Reset the pointer to the BOF (reinitialize). cout << endl << endl << "After reding character by character file shows," << endl; while (fp) { fp.getline(contents, MAXLEN); cout << contents << endl; } fp.close(); // Close the file at last. cout << endl << endl << "Program over. Press any key to exit..."; getch(); return 0; // Successful termination. }