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

  • Home
  • SEARCH
  • 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 8671699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:00:53+00:00 2026-06-12T19:00:53+00:00

My code comparing 2 file sizes seems to behave as if keyfile > sourcefile

  • 0

My code comparing 2 file sizes seems to behave as if keyfile > sourcefile. Is there something missing from the code below and what can I change? The two files I’m using for testing are 3kb keyfile and 14kb sourcefile, which should activate the last if statement provided below.

/* Get size of sourcefile. */
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");
return (1);
}

fflush(sourcefile);
fstat(fileno(sourcefile), &statbuf);
fclose(sourcefile);

/* Get size of keyfile. */
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");
return(1);

fflush(keyfile);
fstat(fileno(keyfile), &keybuf);
fclose(keyfile);
}

/* Open necessary files. */
keyfile=fopen(argv[3], "rb");
sourcefile=fopen(argv[1], "rb");
destfile=fopen(argv[2], "wb");

/* 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");
    }
}   
  • 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-12T19:00:55+00:00Added an answer on June 12, 2026 at 7:00 pm

    It’s because you only fstat the keyfile if it fails to open. Move your curly brace up. Also, as an optimisation of forms, don’t open the file, stat it, close it and re-open it. Just don’t close it and carry on.

    So, since you asked

    /* Get size of sourcefile. */
    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");
    return (1);
    }
    
    fflush(sourcefile);
    //fstat(fileno(sourcefile), &statbuf);   // <-- this is not needed
    //fclose(sourcefile);                    // <-- this is not needed
    
    /* Get size of keyfile. */
    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");
    return(1);
        }                                     // <-- this brace is new (well, moved) (1)
    
    fflush(keyfile);
    //fstat(fileno(keyfile), &keybuf);    // <-- not needed
    //fclose(keyfile);                    // <-- not needed
    //}                                     // <-- this brace has moved up 4 lines to (1)
    
    /* Open necessary files. */
    keyfile=fopen(argv[3], "rb");
    sourcefile=fopen(argv[1], "rb");
    destfile=fopen(argv[2], "wb");
    
    /* 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");
        }
    }   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I generate some code using CXF from a WSDL-file. When compiling the code with
There exist tools for comparing code against a custom specified set of coding guidelines/standards
I'm comparing the following code in C++ and C# and C# (Mono 2.4) seems
I made this code into an executable with py2exe: # File: zipfile-example-1.py from Tkinter
Why comparing to null is so unstable? Just code. IronRuby 0.9.4.0 on .NET 2.0.50727.4927
I need some comparing and replacing code in PHP Our mission is to empower
Possible Duplicate: Comparing Strings in Cocoa Why is this code not recognising the NSString
When compiling the code below with cl /c /clr /W4 the compiler says warning
I recently moved from Delphi 7 to Delphi XE3. A dll file that was
I'm having many problems trying to load an image file (PIXELIMAGEFORMAT). The code does

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.