Advertisement
tegusta

Rubrica (file testo)

Apr 12th, 2012
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.63 KB | None | 0 0
  1. /*
  2. Studente : Scia Massimiliano
  3. Classe : 3IC
  4. Data : 12/04/12 15:51
  5. Nome del file : rubrica disordinata, file di testo
  6. */
  7.  
  8. #include <iostream>
  9. #include <cmath>
  10. #include <ctime>
  11. #include <cstdlib>
  12. #include <cctype>
  13. #include <windows.h>
  14. #include <fstream>
  15. #include <time.h>
  16. #include <string>
  17. #include <stdio.h>
  18. #define N 100
  19.  
  20. using namespace std;
  21.  
  22. void bg_col(){
  23.     system("color 71");
  24. }//bg_col
  25.  
  26. void end(void){
  27.     fflush(stdin);
  28.     cout<<"\n\nPremere Invio per continuare.";
  29.     getchar();
  30. }//end
  31.  
  32. void ins_address(char* fileName, char* mode){
  33.      char contact[N];
  34.      FILE* myFile;
  35.      myFile=fopen(fileName,mode);
  36.      cout<<"\nInsert contacts as the example (surname , name , number)";
  37.      cout<<"\nEx...'Cavani , Edinson , 098727621'";
  38.      cout<<"\nPress ' . ' to end\n";
  39.      fflush(stdin);
  40.      cin.get(contact,N,'\n');
  41.      fflush(stdin);
  42.      while(strcmp(contact,".") != 0){
  43.           fputs(contact, myFile);
  44.           fputc('\n', myFile);
  45.           fflush(stdin);
  46.           cin.get(contact,N,'\n');
  47.           fflush(stdin);
  48.      }//while
  49.      fclose(myFile);
  50. }//ins_address
  51.  
  52.  
  53. bool is_created(char* fileName){
  54.      if(fopen(fileName,"r")==NULL)
  55.          return false;
  56.      else
  57.          return true;
  58. }//is_created
  59.  
  60.  
  61.  
  62. int main(){
  63.     char fileName[N];
  64.     char mode[N];
  65.     bg_col();
  66.     fflush(stdin);
  67.     cout<<"\nInsert filename (with .txt extension):";
  68.     cout<<"\nEx. myFile.txt\n- ";
  69.     cin.get(fileName,N,'\n');
  70.     fflush(stdin);
  71.     bool b=is_created(fileName);
  72.     char c,cc;
  73.     if(b){
  74.         cout<<"\n\nThe file already exist, overwrite? (s / n)";
  75.         cout<<"\n\n o = overwrite, q = write in queue, c = create new";
  76.         cout<<"\n- ";
  77.         do{
  78.           cin>>c;
  79.           if(c!='o' && c!='q' && c!='c')
  80.              cout<<"\nChoice error\n- ";
  81.         }//do
  82.         while(c!='o' && c!='q' && c!='c');
  83.         if(c=='o')
  84.               ins_address(fileName, "w");
  85.         else if(c=='q')
  86.               ins_address(fileName, "a");
  87.         else{
  88.               strcat(fileName,"_1");
  89.               ins_address(fileName, "w");
  90.         }//else
  91.     }//if
  92.     else{
  93.         cout<<"\nThe file searched is not available";
  94.         cout<<"\n\nCan i create a new one? (s/n)";
  95.         cout<<"\n\n y = yes, n = no";
  96.         cout<<"\n\n- ";
  97.         do{
  98.            cin>>cc;
  99.            if(cc!='y' && cc!='n')
  100.                cout<<"\nChoice error\n- ";
  101.         }//do
  102.         while(cc!='y' && cc!='n');
  103.         if(cc=='y')
  104.               ins_address(fileName, "w");
  105.         else
  106.               cout<<"\n\nAloha! :)";
  107.     }//else
  108.     end();
  109.     return 0;
  110. }//main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement