1

I'm currently writing a program that will copy or append one file's text into another. My problem comes up when the user is prompted whether they want to overwrite or append the file, scanf() and getchar() are both skipped. I have tried using numerous combinations of getchar()'s and scanf()'s along with fflush(stdin) and ensuring that all the files I had opened are close, but I still cannot input a selection.

The specific section of code containing the first prompt is here.

`/****************PROMPT FOR OVERWRITE****************/
printf("Would you like to overwrite the Destination File?\n");
printf("1=NO,2=YES=");
scanf("%d", &overwriteAnswer);
    if(overwriteAnswer == 2)
    {
`

This scanf() or, when I used getChar(), is just skipped and is usually filled with a different negative number every time the code is executed.

Full code follows

    if((infile = open(argv[1], O_RDONLY)) == 0)
    {
        /****************INPUT FILE OPENED****************/
        printf("%s open\n",argv[1]);
        if ((outfile = access(argv[1], F_OK)) == 0 )
        {
            /****************PROMPT FOR OVERWRITE****************/
            printf("Would you like to overwrite the Destination File?\n");
            printf("1=NO,2=YES=");
            scanf("%d", &overwriteAnswer);
            if(overwriteAnswer == 2)
            {
                printf("Overwriting Destination File\n");
            }

Any help or advice is greatly appreciated.

1 Answers1

1

I do not understand why you do not use fflush like that:

                printf("\nWould you like to append the Destination File?\n");
                printf("1=NO,2=YES=");
                fflush(stdin);
                scanf("%d", &appendAnswer);

EDIT:

if fflush(stdin) does not work, try to enforce scanf to read numbers in the following way:

    // additional variables
    char ch; // just a char to read from stream
    int wasNumber; // flag of successful scanf execution
    do{
        wasNumber = 0;
        // ask for input
        printf("\nWould you like to append the Destination File?\n");
        printf("1=NO, 2=YES : ");
        // read mumber
        wasNumber = scanf("%d", &appendAnswer);
        // clean the input bufer if it has not number
        if( wasNumber == 0 )
        {
            while( getchar() != '\n' ); // read till the end of line
        }
    }while(wasNumber != 1 || appendAnswer < 1 || appendAnswer > 2);
VolAnd
  • 6,367
  • 3
  • 25
  • 43
  • I have, it didn't solve anything for me. Just tried again to double check. – user3006826 Feb 03 '15 at 20:53
  • Also, there are only two scanf()'s in the code, the first one is also being skipped. – user3006826 Feb 03 '15 at 20:55
  • Have you tried debbuger to sure that program can reach the scanf ? – VolAnd Feb 03 '15 at 20:58
  • I have done gcc with the debugger and nothing shows up. Both print statements for overwrite and append are printed in fulled. If I print out the value of overwriteAnswer, the first scanf(), it is a different negative number every time. I'm positive it is being reached. – user3006826 Feb 03 '15 at 21:13
  • No, that is the problem. It doesn't stop at either of the scanf()'s. – user3006826 Feb 03 '15 at 21:18
  • I have one more (the last) idea how to force scanf to work. See EDIT in my answer – VolAnd Feb 04 '15 at 06:45