Image

Programs - C++ - File Handling

Program:


#include <iostream.h>
#include <fstream.h>
#include <string.h>
//using namespace std;

void main() {
int choice,i;
cout << "Save or read data? (1 or 0): ";
cin >> choice;
if(choice>0) {
  char text[10], password[10], file[20];
  cout << "Enter your text: ";
  cin >> text;
  cout << "Enter your password: ";
  cin >> password;
  cout << "Name of the file: ";
  cin >> file;
  for(i=0; i<(int)text.size(); ++i)
   text[i]-=64;
  for(i=0; i<(int)password.size(); ++i)
   password[i]-=64;
  ofstream fout(file);
  fout << password << 'z' << text;
  fout.close();
 } else {
  char password[10], file[20], c_password[10], c_text[10];
  cout << "Name of the file: ";
  cin >> file;
  ifstream fin(file);
  getline(fin, c_password, 'z');
  getline(fin, c_text);
  fin.close();
  cout << "Password: ";
  cin >> password;
  for(int i=0; i<(int)c_password.size(); ++i)
   c_password[i]+=64;
  if(password==c_password) {
   for(int i=0; i<(int)c_text.size(); ++i)
    c_text[i]+=64;
   cout << c_text;
  }
 }
getch();
}