Advertisement
sidex15

C++ fstream Edit File/Database

Apr 28th, 2019 (edited)
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.53 KB | None | 0 0
  1. /* fstream Edit File/Database By Moses Limbo
  2. */
  3. #include <conio.h>
  4. #include <stdio.h>
  5. #include <iostream.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <fstream.h>
  9.  
  10. struct{
  11.  char user[20],pass[20],name[50],address[50],course[20],section[20];
  12. }mehh[50];
  13.  
  14. char input[50];
  15. int ctr=0,opt=0,len=0,flag=0,eopt=0;
  16.  
  17. void load(),save(),space(),unspace(),login(),reg(),view(),edit(),loginmenu(),modify();
  18.  
  19. void main(){
  20.  clrscr();
  21.  load();
  22.  cout<<"[1] Login\n[2] Register\n[3] Exit\n";
  23.  cin>>opt;
  24.  switch (opt){
  25.   case 1: login(); break;
  26.   case 2: reg(); break;
  27.   default: exit(0);
  28.  }
  29. }
  30.  
  31. void login(){
  32.  char luser[20],lpass[20];
  33.  clrscr();
  34.  cout<<"Username: ";
  35.  cin>>luser;
  36.  cout<<"Password: ";
  37.  cin>>lpass;
  38.  for (int m=0;m<ctr;m++){
  39.   if(!strcmp(luser,mehh[m].user)&&!strcmp(lpass,mehh[m].pass)){
  40.    loginmenu();
  41.    flag=1;
  42.   }
  43.  }
  44.  if(flag==0){
  45.   cout<<"Incorrect Username or Password...";
  46.   getch();
  47.   main();
  48.  }
  49. }
  50.  
  51. void reg(){
  52.  clrscr();
  53.  cout<<"Username: ";
  54.  cin>>mehh[ctr].user;
  55.  cout<<"Password: ";
  56.  cin>>mehh[ctr].pass;
  57.  cout<<"Name: ";
  58.  unspace();
  59.  strcpy(mehh[ctr].name,input);
  60.  cout<<"Address: ";
  61.  unspace();
  62.  strcpy(mehh[ctr].address,input);
  63.  cout<<"Course: ";
  64.  cin>>mehh[ctr].course;
  65.  cout<<"Section: ";
  66.  cin>>mehh[ctr].section;
  67.  cout<<"Login Successful";
  68.  getch();
  69.  ctr++;
  70.  save();
  71.  main();
  72. }
  73.  
  74. void loginmenu(){
  75.  clrscr();
  76.  cout<<"[1] View\n[2] Edit\n[3] Logout";
  77.  cin>>opt;
  78.  switch(opt){
  79.   case 1: view(); break;
  80.   case 2: edit(); break;
  81.   case 3: main(); break;
  82.   default: main();
  83.  }
  84. }
  85.  
  86. void view(){
  87.  clrscr();
  88.  for(int n=0; n<ctr; n++){
  89.   cout<<"Id: "<<n+1<<endl<<endl;
  90.   cout<<"Username: "<<mehh[n].user<<endl;
  91.   cout<<"Password: "<<mehh[n].pass<<endl;
  92.   cout<<"Name: ";
  93.   strcpy(input,mehh[n].name);
  94.   space();
  95.   cout<<"Address: ";
  96.   strcpy(input,mehh[n].address);
  97.   space();
  98.   cout<<"Course: "<<mehh[n].course<<endl;
  99.   cout<<"Section: "<<mehh[n].section<<endl;
  100.   cout<<endl;
  101.  }
  102.  getch();
  103.  loginmenu();
  104. }
  105.  
  106. void edit(){
  107.  clrscr();
  108.  for(int i=0;i<ctr;i++){
  109.   cout<<i+1<<"\t"<<mehh[i].user<<endl;
  110.  }
  111.  cout<<"Enter ID: ";
  112.  cin>>eopt;
  113.  eopt-=1;
  114.  clrscr();
  115.  cout<<"Edit: ";
  116.  cout<<"1. Username\n2. Password\n3. Name\n4. Address\n5. Course\n6. Section\n";
  117.  cin>>opt;
  118.  switch(opt){
  119.   case 1: cout<<"Username: ";
  120.       gets(mehh[eopt].user);
  121.       cout<<"Edit successful...";
  122.       getch();
  123.       modify(); //modifies the file
  124.       main(); break;
  125.   case 2: cout<<"Password: ";
  126.       gets(mehh[eopt].pass);
  127.       cout<<"Edit successful...";
  128.       getch();
  129.       modify(); //modifies the file
  130.       main(); break;
  131.   case 3: cout<<"Name: "; unspace();
  132.       strcpy(mehh[eopt].name,input);
  133.       cout<<"Edit successful...";
  134.       getch();
  135.       modify(); //modifies the file
  136.       main(); break;
  137.   case 4: cout<<"Address: "; unspace();
  138.       strcpy(mehh[eopt].address,input);
  139.       cout<<"Edit successful...";
  140.       getch();
  141.       modify(); //modifies the file
  142.       main(); break;
  143.   case 5: cout<<"course: ";
  144.       gets(mehh[eopt].course);
  145.       cout<<"Edit successful...";
  146.       getch();
  147.       modify(); //modifies the file
  148.       main(); break;
  149.   case 6: cout<<"section: ";
  150.       gets(mehh[eopt].section);
  151.       cout<<"Edit successful...";
  152.       getch();
  153.       modify(); //modifies the file
  154.       main(); break;
  155.   default: main();
  156.  }
  157. }
  158.  
  159. void load(){
  160.   ifstream read;
  161.   read.open("mehh.txt");
  162.   read>>ctr;
  163.   for (int i=0;i<ctr;i++){
  164.    read>>mehh[i].user>>mehh[i].pass>>mehh[i].name>>mehh[i].address>>mehh[i].course>>mehh[i].section;
  165.   }
  166.   read.close();
  167. }
  168.  
  169. void save(){
  170.  ofstream write;
  171.  write.open("mehh.txt");
  172.  write<<ctr<<endl;
  173.  for (int j=0;j<ctr;j++){
  174.  write<<mehh[j].user<<" "<<mehh[j].pass<<" "<<mehh[j].name<<" "<<mehh[j].address<<" "<<mehh[j].course<<" "<<mehh[j].section<<endl;
  175.  }
  176.  write.close();
  177. }
  178.  
  179. void modify(){ //function to edit the file
  180.  ofstream write;
  181.  write.open("mehh.txt");
  182.  write<<ctr<<endl;
  183.  for (int j=0;j<ctr;j++){
  184.  if(j==eopt){ //to match what id to replace and overwrite the certain line...
  185.   write<<mehh[eopt].user<<" "<<mehh[eopt].pass<<" "<<mehh[eopt].name<<" "<<mehh[eopt].address<<" "<<mehh[eopt].course<<" "<<mehh[eopt].section<<endl;
  186.   }
  187.  else{ //if it doesn't match
  188.    write<<mehh[j].user<<" "<<mehh[j].pass<<" "<<mehh[j].name<<" "<<mehh[j].address<<" "<<mehh[j].course<<" "<<mehh[j].section<<endl;
  189.   }
  190.  }
  191.  write.close();
  192. }
  193.  
  194. void unspace(){
  195.  gets(input);
  196.  len=strlen(input);
  197.  for (int k=0;k<len;k++){
  198.   if (input[k]==' '){
  199.    input[k]='_';
  200.   }
  201.  }
  202. }
  203.  
  204. void space(){
  205.  len=strlen(input);
  206.  for (int l=0;l<len;l++){
  207.   if (input[l]=='_'){
  208.    input[l]=' ';
  209.   }
  210.  }
  211.  cout<<input<<endl;
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement