How to read past EOF from getc?

735 views Asked by At

I am writing a XOR encryption program which works fine during encryption but during decryption the

char ca2=fgetc(f);

gets stuck at one point and no decryption takes place after that my best guess about the problem is (the encrypted file contains all sorts of characters ) as soon as fgetc reaches EOF mark which can be present before the actual end of the file it gets stuck there and stop reading the next characters .

is this some kind of limitation of getc() ? here is my rubbish code

int get_file_size(char  filename[]) 
{
    FILE *p_file = NULL;
    p_file = fopen(filename,"rb");
    fseek(p_file,0,SEEK_END);
    int size = ftell(p_file);
    fclose(p_file);
    return size;
}

int endec(char filename[],char psdw[])
{
    FILE *f;
    int hashed=0,ed=0;
    int inphash=inhash(psdw);
    inphash=inphash%50;
    f=fopen(filename,"r");
    if(f==NULL)
    printf("failed");

    char temps[999999];
    long int crs=0,j=0;
    int filesz=get_file_size(filename);
    printf("file size = %d\n\n",filesz);
while(1){

    inphash=inphash+2;
    char ca=(char)inphash;
    char ca2=fgetc(f);
    printf("%c\n",ca2);
    if(crs>=filesz)
        break;
    temps[crs]= ca2 ^ ca;
    crs++;

        }
fclose(f);
printf("%d",strlen(temps));

FILE *fp;
fp=fopen(filename,"wt");
for(j=0;j<crs;j++){
        putc (temps[j] , fp);
printf("%c",temps[j]);

}
fclose(fp);
}
1

There are 1 answers

0
Joe Z On BEST ANSWER

Your problem is right here:

f=fopen(filename,"r");

You open the file for text reading, not for binary. Your file size function gets it right, but your decoder function does not.

The idiomatic way to read a file character by character using the C-style IO routines is like this:

f = fopen(filename, "rb");

if (!f)
    // handle error

int c;   // NOTE:  int, not char!

while ( (c = fgetc(f)) != EOF )
{
    // do something with 'c'
}

This idiom does not require you to get the file size as a separate operation. You can rewrite your XOR "encryption" routine with a simple loop of the above form. It will be much clearer and more concise.

Your entire decoder function could be rewritten as follows: (minus the debug code)

int endec(char filename[], char psdw[])
{
     int inphash = inhash(psdw) % 50;
     char temp[999999];  // really, should be std::vector<char>
     FILE *f;

     if ( (f = fopen(filename, "rb")) == NULL )
     {
         printf("opening for read failed\n");
         return -1;
     }

     size_t crs = 0;
     int    c;

     while ( (c = fgetc(f)) != EOF )
     {
         inphash += 2;
         temp[crs++] = (char)(inphash ^ c);
     }
     fclose(f);

     if ( (f = fopen(filename, "wt")) == NULL )
     {
         printf("opening for write failed\n");
         return -1;
     }

     if (fwrite(temp, crs, 1, f) != crs)
     {
         printf("short write\n");
         fclose(f);
         return -1;
     }

     fclose(f);
     return 0;
 }

Not stellar error handling, but it is error handling.