-1

I am currently writing a simple little One-time pad program. I want it to warn the user when the keyfile is smaller than the source file, but when I run it my program simply ignores it.

/* Check if keyfile is the same size as, or bigger than the sourcefile */
if(sizeof keyfile < sizeof sourcefile)
{
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");
    }

I suspect the problem lies with my use sizeof, but I don't know about any other options? Any suggestions as to how I can alter my code to make it work?

EDIT: I have tried adding fstat to the best of my abilities, but it still reacts in the same manner... here is what I did:

/* 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);
}

and altered the first line of the if statement being ignored:

if((keybuf.st_size) < (statbuf.st_size))

Why is it still being ignored by the program?

Cœur
  • 37,241
  • 25
  • 195
  • 267
youjustreadthis
  • 622
  • 3
  • 9
  • 24
  • 1
    http://stackoverflow.com/questions/238603/how-can-i-get-a-files-size-in-c – Paul Oct 12 '12 at 16:20
  • Since the decision is made at compile time, showing the correct declarations for the variables `keyfile` and `sourcefile` probably won't matter, but for completeness, you should do that. – Jonathan Leffler Oct 12 '12 at 16:21
  • 1
    Considere creating another question if you want people to see it, and for the sake of readability. – tomahh Oct 12 '12 at 19:45

4 Answers4

10

sizeof does not return the size of a file. sizeof is use to check the number of bytes a type takes in memory.

Look at this post if you want more information on getting a file's size.

Community
  • 1
  • 1
tomahh
  • 13,441
  • 3
  • 49
  • 70
5

sizeof is a compile-time operator yielding the size of its operands' data type in bytes. Are you sure that's what you want to do here? I very much doubt that, as it seems you want to compare file sizes. (fopen(), fseek() to end, ftell() to get the size of the file, or whatever more comfortable function your OS' API provides you with.)

DevSolar
  • 67,862
  • 21
  • 134
  • 209
4

sizeof is a unary operator used to calculate the size of a datatype. You cannot use it to find/compare the size of text files.

Raj
  • 4,342
  • 9
  • 40
  • 45
3

You need to use stat to get the size of a file.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
  • How to get the file size depends on the platform, of course...that's why it isn't covered by the C standard. For POSIX-ish systems, `stat` is correct. For other platforms, the answer varies by platform. – Jonathan Leffler Oct 12 '12 at 16:24
  • 2
    @DevSolar - True if Windows. But Windows does support POSIX - and stat is POSIX. Just makes it easier to port as well to UNIX (as well as a few other Operating systems) – Ed Heal Oct 12 '12 at 16:25