Thread: ERROR in gets

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

    ERROR in gets

    Code:
    /* 
    Name: CRYDEC TOKI
    Author: KRAKZ 
    Date: 25/02/03 23.08 
    Description: CRYPTA E DECRYPTA
    LICENZA : GNU|GPL 
    VERSIONE : 1.0
    */ 
    
    int  crypt ()   ;
    int   decrypt () ;
    
    
    
    
    # include <stdio.h> 
    # include <string.h> 
    # include <stdlib.h> 
    # define MAX 255 
    
    
    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 o 3 \n");
    scanf  ("%d" , &I )          ;  
    
    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;
    
      printf ("Benvenuto su crypt 0.5\n") ;
    
          
          
      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 1.0\n\n" ) ;
      fprintf(cfPtr, "\n\n\nwww.toki.it\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] ; 
    
    
    
      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") ; 
    
    system("PAUSE"); 
    
    }

    i dont know , me programm jump first gets
    http://linuxdesktop.it

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Never use gets()... always use fgets() instead. check your helpfiles/text books/FAQ for specifics.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    44
    fgets () ???


    in my book not this command

    and try this command in my compiler , but have many error


    http://linuxdesktop.it

  4. #4

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    44
    ok underastund


    the structur of fgets() is different the gets
    http://linuxdesktop.it

  6. #6
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    It is best not to use scanf and gets in the same program. The 2 do not work well with each other. It would be better to use gets all the time and use atoi or atof to convert to int/double if needed.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >ERROR in gets
    gets is an error

    -Prelude
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Dec 2002
    Posts
    44
    why ???


    gets is error ???
    http://linuxdesktop.it

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

    Unhappy

    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 
    
    
    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 o 3 \n");
    scanf  ("%d" , &I )          ;  
    
    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 , *apri ;
    
      printf ("Benvenuto su crypt 0.5\n") ;
       
       
      if ((apri = fopen( "temp.txt" , "a")) == NULL)
                        
                        printf("File gia aperto\n");
      
           
      printf ("Inserisci frase\n") ; 
      fgets(frase , sizeof(frase) ,  apri) ; 
      printf ("Inserisci chiave\n") ; 
      fgets(chiave , sizeof(chiave) , apri) ;
      printf ("Salva con nome\n")  ;
      fgets(nomefile , sizeof(nomefile) , apri) ;
      
    
      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 1.0\n\n" ) ;
      fprintf(cfPtr, "\n\n\nwww.toki.it\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] ; 
    
    
    
      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") ; 
    
    system("PAUSE"); 
    
    }
    I used fgets ;(
    my programm crushing
    http://linuxdesktop.it

  10. #10
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Code:
    if ((apri = fopen( "temp.txt" , "a")) == NULL)
                        
                        printf("File gia aperto\n");
      
           
      printf ("Inserisci frase\n") ; 
      fgets(frase , sizeof(frase) ,  apri) ; 
      printf ("Inserisci chiave\n") ; 
      fgets(chiave , sizeof(chiave) , apri) ;
      printf ("Salva con nome\n")  ;
      fgets(nomefile , sizeof(nomefile) , apri) ;
    You open the file for appending and then read from the file. I'm sure the fgets function will return a NULL pointer...

Popular pages Recent additions subscribe to a feed