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.