Scanf che viene 'saltata'

di Anonimizzato20305 il
1 risposte
Salve,sto facendo un programma molto semplice,che sostituisce un carattere in un file con un altro.
Il codice compila senza errori,ma quando viene chiesto all'utente di inserire i due caratteri, il secondo
scanf non so perchè viene 'saltato'

Allego il codice

//Scrivere un programma che sostituisca in un file di testo un carattere con un altro

#include <stdio.h>
#include <stdlib.h>

int main()
{

    FILE *f1,*f2;
    char car1;
    char car2;
    int ch;

    printf("\nInserisci il carattere da sostituire: ");
    scanf("%c",&car1);

	//ERRORE QUI
    printf("\nInserisci il carattere con cui sostituire: ");
    scanf("%c",&car2);


    f1 = fopen("testo1.txt","r");
    if(f1==NULL){
        printf("\nErrore");    }


    f2 = fopen("temp.txt","w");
    if(f2==NULL){
        printf("\nErrore");    }


    while( (ch= getc(f1)) != EOF ){
        if( ch != car1)
            putc(ch,f2);
        else
            putc(car2,f2);

    }






    fclose(f1);
    fclose(f2);

       return 0;
}

1 Risposte

  • Esistono tanti thread sull'argomento nel forum...

    Cerca problemi su scanf e buffer di input ....
Devi accedere o registrarti per scrivere nel forum
1 risposte