Thread: CRYPT don' t save file

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    44

    CRYPT don' t save file

    this is full code
    Code:
    /* 
    Name: CRYDEC TOKI
    Author: KRAKZ 
    Date: 25/02/03 23.08 
    Description: CRYPTA E DECRYPTA
    LICENZA : GNU|GPL 
    VERSIONE : 1.0
    */ 
    
    int  crypt (void)   ;
    int   decrypt (void) ;
    
    
    
    
    # include <stdio.h> 
    # include <string.h> 
    # include <stdlib.h> 
    # define MAX 255 
    
    
    int main ()
    
     {   int I;
         printf ("                *** BENVENUTO SU CRYDEC VERSIONE 1.0 ***\n\n\n") ;
         printf ("SELEZIONA UNA DELLE OPZIONI\n\n")          ;
         printf ("1 - Crypta\n")      ;
         printf ("2 - Decrypta\n")    ;
         printf ("3 - Info autori\n") ;
         printf ("Digita 1-2-3-4\n");
         while (( I = getchar ()) != EOF)
           
           {
    
             switch(I) {       
    
                                 case '1': crypt () ;
                                 break         ;
    
                                 case '2': decrypt () ;
                                 break           ;
        
                                 case '3': printf ("Krakz") ;
                                 break         ;
                                 
                               
                        } 
           }
    }
    
    
    int  crypt ()
    { 
      int    i, I = 0 , countchiave , countfrase , crypt[MAX]  ; 
      
      char frase[MAX] , chiave[MAX] , nomefile[10] ; 
      
      FILE *cfPtr;
    
    
    
          
      getchar () ;   
      printf ("Inserisci frase\n") ; 
      gets(frase) ; 
      printf ("Inserisci chiave\n") ; 
      gets(chiave) ; 
      printf ("Salva con nome\n")  ;
      gets(nomefile) ;
    
      countfrase = strlen(frase) ;       /* VERIFICA LA LUNGHEZZA DELLE STRINGHE */
      countchiave = strlen(chiave) ; 
    
      for ( i = 0 ; countfrase > i ; i++) /* CONTROLLA CHE TUTTA LA STRINGA SIA CRIPTATA */
       
       
           { 
    
             if ( countchiave < I)  I = 0 ;;   /* CONTROLLA SE SONO STATE UTILIZZATE TUTTE LE LETTERE DELLA CHIAVE 
                                        SE VEDE CHE SONO STATE USATE TUTTE , RESETTA IL TUTTO PARTENDO 
                                        DALLA PRIMA LETTERA */
    
                         crypt[i] = chiave[I] + frase[i] ; /* CRIPTA LA STRINGA */
    
            if ((cfPtr = fopen( nomefile , "a")) == NULL)
                        
                        printf("File gia aperto\n");
    
    
    
            if ( crypt[i] < 100 )    fprintf(cfPtr, "0" ) ;;   /* VERIFICA CHE IL VALORE DATO SIA MAGGIORE DI 100
                                                         E SE RISCONTRA UN VALORE MINORE DI 100 AGGIUNGE
                                                         IL VALORE 0 */
            fprintf(cfPtr, "%d",  crypt[i] );                /* STAMPA LA FRASE CRIPTATA */
                                          
                     
            I = 1 + I ;                                      /* REALIZZA UNO SHIFT A DESTRA */
     
            } 
      fprintf(cfPtr, "\n\n\nUsa decrypt per vedere il testo\n\n" ) ;
      fprintf(cfPtr, "\n\n\nVersione programma 0.5\n\n" ) ;
      printf ("Il file testo è stato cryptato , per decryptarlo\n") ; 
      
      system("PAUSE");                            /* COMANDO COMPATIBILE SOLO CON OS WIN , RICHIEDE LA 
                                                    PRESSIONE DI UN TASTO PER CONTINUARE L'ESECUZIONE 
                                                    DEL PROGRAMMA*/
    
    }
    
    int   decrypt ()
    { 
      
      int I=0 , i , countfrase , countchiave , dEcrypt[MAX] , de = 0 ; 
      char frase[MAX] , chiave[MAX] ; 
    
    
      getchar() ; 
      printf ("Inserisci File Cryptato\n") ; 
      gets(frase) ; 
      printf ("Inserisci chiave\n") ; 
      gets(chiave) ; 
    
    
      countfrase = strlen(frase) ; 
      countchiave = strlen(chiave) ; 
    
      for ( i = 0 ; countfrase > i ; i = 3 + i) 
                
          { 
              if ( countchiave < I) 
                           I = 0 ;; 
    
              dEcrypt[de] = (frase[i]* 100 + frase [i+1] * 10 + frase [i+2] +48 ) - chiave [I] ; 
              
              printf ("%c" , dEcrypt[I]) ; 
              I = 1 + I ; 
    
    
           }
           
             
    printf ("\n") ; 
    
    
    
    }
    this is crypt

    Code:
    int  crypt ()
    { 
      int    i, I = 0 , countchiave , countfrase , crypt[MAX]  ; 
      
      char frase[MAX] , chiave[MAX] , nomefile[10] ; 
      
      FILE *cfPtr;
    
    
    
          
      getchar () ;   
      printf ("Inserisci frase\n") ; 
      gets(frase) ; 
      printf ("Inserisci chiave\n") ; 
      gets(chiave) ; 
      printf ("Salva con nome\n")  ;
      gets(nomefile) ;
    
      countfrase = strlen(frase) ;       /* VERIFICA LA LUNGHEZZA DELLE STRINGHE */
      countchiave = strlen(chiave) ; 
    
      for ( i = 0 ; countfrase > i ; i++) /* CONTROLLA CHE TUTTA LA STRINGA SIA CRIPTATA */
       
       
           { 
    
             if ( countchiave < I)  I = 0 ;;   /* CONTROLLA SE SONO STATE UTILIZZATE TUTTE LE LETTERE DELLA CHIAVE 
                                        SE VEDE CHE SONO STATE USATE TUTTE , RESETTA IL TUTTO PARTENDO 
                                        DALLA PRIMA LETTERA */
    
                         crypt[i] = chiave[I] + frase[i] ; /* CRIPTA LA STRINGA */
    
            if ((cfPtr = fopen( nomefile , "a")) == NULL)
                        
                        printf("File gia aperto\n");
    
    
    
            if ( crypt[i] < 100 )    fprintf(cfPtr, "0" ) ;;   /* VERIFICA CHE IL VALORE DATO SIA MAGGIORE DI 100
                                                         E SE RISCONTRA UN VALORE MINORE DI 100 AGGIUNGE
                                                         IL VALORE 0 */
            fprintf(cfPtr, "%d",  crypt[i] );                /* STAMPA LA FRASE CRIPTATA */
                                          
                     
            I = 1 + I ;                                      /* REALIZZA UNO SHIFT A DESTRA */
     
            } 
      fprintf(cfPtr, "\n\n\nUsa decrypt per vedere il testo\n\n" ) ;
      fprintf(cfPtr, "\n\n\nVersione programma 0.5\n\n" ) ;
      printf ("Il file testo è stato cryptato , per decryptarlo\n") ; 
      
      system("PAUSE");                            /* COMANDO COMPATIBILE SOLO CON OS WIN , RICHIEDE LA 
                                                    PRESSIONE DI UN TASTO PER CONTINUARE L'ESECUZIONE 
                                                    DEL PROGRAMMA*/
    
    }
    http://linuxdesktop.it

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    ... and your question is ....

    (please post only the code that is relevant to the problem)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    44
    my problem is

    Code:
    int  crypt ()
    { 
      int    i, I = 0 , countchiave , countfrase , crypt[MAX]  ; 
      
      char frase[MAX] , chiave[MAX] , nomefile[10] ; 
      
      FILE *cfPtr;
    
    
    
          
      getchar () ;   
      printf ("Inserisci frase\n") ; 
      gets(frase) ; 
      printf ("Inserisci chiave\n") ; 
      gets(chiave) ; 
      printf ("Salva con nome\n")  ;
      gets(nomefile) ;
    
      countfrase = strlen(frase) ;       /* VERIFICA LA LUNGHEZZA DELLE STRINGHE */
      countchiave = strlen(chiave) ; 
    
      for ( i = 0 ; countfrase > i ; i++) /* CONTROLLA CHE TUTTA LA STRINGA SIA CRIPTATA */
       
       
           { 
    
             if ( countchiave < I)  I = 0 ;;   /* CONTROLLA SE SONO STATE UTILIZZATE TUTTE LE LETTERE DELLA CHIAVE 
                                        SE VEDE CHE SONO STATE USATE TUTTE , RESETTA IL TUTTO PARTENDO 
                                        DALLA PRIMA LETTERA */
    
                         crypt[i] = chiave[I] + frase[i] ; /* CRIPTA LA STRINGA */
    
            if ((cfPtr = fopen( nomefile , "a")) == NULL)
                        
                        printf("File gia aperto\n");
    
    
    
            if ( crypt[i] < 100 )    fprintf(cfPtr, "0" ) ;;   /* VERIFICA CHE IL VALORE DATO SIA MAGGIORE DI 100
                                                         E SE RISCONTRA UN VALORE MINORE DI 100 AGGIUNGE
                                                         IL VALORE 0 */
            fprintf(cfPtr, "%d",  crypt[i] );                /* STAMPA LA FRASE CRIPTATA */
                                          
                     
            I = 1 + I ;                                      /* REALIZZA UNO SHIFT A DESTRA */
     
            } 
      fprintf(cfPtr, "\n\n\nUsa decrypt per vedere il testo\n\n" ) ;
      fprintf(cfPtr, "\n\n\nVersione programma 0.5\n\n" ) ;
      printf ("Il file testo è stato cryptato , per decryptarlo\n") ; 
      
      system("PAUSE");                            /* COMANDO COMPATIBILE SOLO CON OS WIN , RICHIEDE LA 
                                                    PRESSIONE DI UN TASTO PER CONTINUARE L'ESECUZIONE 
                                                    DEL PROGRAMMA*/
    
    }
    http://linuxdesktop.it

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No, that's your source code. What:
    a) is it supposed to do?
    b) does it do that it shouldn't?
    c) doesn't do that it should?
    d) are you stuck on?


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    gets is evil (read the bugs section on the manual page) - use fgets instead...
    At the end of crypt() you need to close your file...
    Code:
    if ((cfPtr = fopen( nomefile , "a")) == NULL)
    {                    
       printf("File gia aperto\n");
       return -1;
    }

  6. #6
    Registered User
    Join Date
    Dec 2002
    Posts
    44
    Originally posted by quzah
    No, that's your source code. What:
    a) is it supposed to do?
    b) does it do that it shouldn't?
    c) doesn't do that it should?
    d) are you stuck on?


    Quzah.
    a) - 1 insert string (this is ok)
    - 2 insert key of crypt (this is ok )
    - 3 insert name of file save (this is ok)
    - 4 save crypt file (this is bad)

    b) - save as file but dont write in here (file is void)

    c) - created file but dont write in here (file is void)

    d) ????
    http://linuxdesktop.it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Ask user to save file before exiting.
    By Bajanine in forum Windows Programming
    Replies: 3
    Last Post: 11-15-2004, 06:34 PM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM