Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8697253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:28:44+00:00 2026-06-13T01:28:44+00:00

When the first scanf() is used and you answer Y, the second scanf() skips

  • 0

When the first scanf() is used and you answer Y, the second scanf() skips straight to “No option selected. Exiting…”. That message also appears when the keyfile is larger than the sourcefile and the last scanf does it’s job properly. So I’m at a loss here, what is wrong? (code compiles nicely so feel free to try)

EDIT: It would be helpful for downvoters to at least post a reason. I am not a very good programmer and just attempting to learn here.

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>

int main(int argc, char **argv)
{
struct stat statbuf;
struct stat keybuf;

int key;
int data;
int output;
int count;
char ans;
FILE * keyfile;
FILE * sourcefile;
FILE * destfile;

if(argc<4)
{
printf("OTP-Bunny 1.0\n");
printf("USAGE: OTP <source file> <output file> <keyfile>\n");
return (0);
}

/* Check number of arguments. */
if(argc>4)
{
printf("Too many arguments.\n");
printf("USAGE: OTP <source file> <output file> <keyfile>\n");
return(1);
}

/* Check if sourcefile can be opened. */
if((sourcefile = fopen(argv[1], "rb"))== NULL)
{
printf("Can't open source file.\n");
printf("Please enter a valid filename.\n");
printf("USAGE: OTP <source file> <output file> <keyfile>\n");
perror("Error");
return (1);
}

/* Get size of sourcefile */
fstat(fileno(sourcefile), &statbuf); 

/* Check if keyfile can be opened. */
if((keyfile = fopen(argv[3], "rb"))== NULL)
{
printf("Can't open keyfile.\n");
printf("Please enter a valid filename.\n"); 
printf("USAGE: OTP <source file> <output file> <keyfile>\n");
perror("Error");
return(1);
}                               

/* Get size of keyfile */
fstat(fileno(keyfile), &keybuf);

/* Check if keyfile is the same size as, or bigger than the sourcefile */
if((keybuf.st_size) < (statbuf.st_size))
{
printf("Source file is larger than keyfile.\n");
printf("This significantly reduces cryptographic strength.\n");
printf("Do you wish to continue? (Y/N)\n");
scanf("%c", &ans);
if(ans == 'n' || ans == 'N')
{
return (1);
}
if(ans == 'y' || ans == 'Y')
{
    printf("Proceeding with Encryption/Decryption.\n");
    }
else
{
printf("No option selected. Exiting...\n");
return (1);
}
}   

/* Check if destfile can be opened. */
if((keyfile = fopen(argv[2], "wb"))== NULL)
{
printf("Can't open output file.\n");
perror("Error");
return(1);                  
}    

/* Open destfile. */
destfile=fopen(argv[2], "wb");

/* Encrypt/Decrypt and write to output file. */
while(count < (statbuf.st_size))
{
key=fgetc(keyfile);
data=fgetc(sourcefile);

output=(key^data);

fputc(output,destfile);
count++;
}

/* Close files. */
fclose(keyfile);
fclose(sourcefile);
fclose(destfile);

printf("Encryption/Decryption Complete.\n");

/* Delete keyfile option. */
printf("Do you wish to delete the keyfile? (Y/N)\n");
scanf("%c", &ans);
if(ans == 'y' || ans == 'Y')
{
if ( remove(argv[3]) == 0)
    {
    printf("File deleted successfully.\n");
    }
else
    {
    printf("Unable to delete the file.\n");
    perror("Error");
    return(1);
    }
}

if(ans == 'n' || ans == 'N')
{
return(0);
}
else
{
printf("No option selected. Exiting...\n");
}
return(0);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T01:28:46+00:00Added an answer on June 13, 2026 at 1:28 am

    Actually, scanf() is behaving correctly, but it is a very tricky function to use correctly and it is easy for your expectations to be different from its documented behaviour.

    Your first scanf() call reads a single character, leaving the newline behind.

    Your second scanf() call reads a newline because that’s next…leading to problems.

    You can fix the code by using " %c"; the leading blank skips (optional) white space.

    In general, the better fix is to use a combination of fgets() or equivalent (readline() in POSIX 2008, perhaps), and sscanf(). Read a line of data into memory with fgets(); analyze it with sscanf(). That way, you can report the error better too; you have all the information that the user typed available for error reporting.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First i selected images from photo library to ALAsset Library and after that i
I used ReconstructMe to scan my first half body (arm and head). The result
How do I determine that mdworker (Spotlight) has completed its first scan? I'm basically
int age; char name[10]; scanf(%d, &age); scanf(%s, name); In first scanf function we use
I have several methods that I've used previously to accept user input and return
I have the following code that is used to turn http URLs in text
In this thread some one commented that the following code should only be used
I have been getting an error message that I can't resolve. It originates from
I have got this code that reads an integer using scanf and checks if
When I give the first input, an extra 0 appears before gets() works. But

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.