GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 14-Sep-2005, 16:32
Lewk of Serthic Lewk of Serthic is offline
New Member
 
Join Date: Sep 2005
Posts: 5
Lewk of Serthic is on a distinguished road

feof() trouble


I'm having some trouble with my little encryption program. It compiles alright but gives me a "Illegal Operation" when I run it, right after I input a key.

CPP / C++ / C Code:
#include <stdio.h>

encrypt(unsigned long* v, unsigned long* k) 
{
     unsigned long v0=v[0], v1=v[1], sum=0, i;           /* set up */
     unsigned long delta=0x9e3779b9;                     /* a key schedule constant */
     unsigned long k0=k[0], k1=k[1], k2=k[2], k3=k[3];   /* cache key */
     for (i=0; i < 32; i++) 
     {                            /* basic cycle start */
         sum += delta;
         v0 += (v1<<4)+k0 ^ v1+sum ^ (v1>>5)+k1;
         v1 += (v0<<4)+k2 ^ v0+sum ^ (v0>>5)+k3;         /* end cycle */
     }
     v[0]=v0; v[1]=v1;
}

main()
{
    char infile[20];
    char outfile[20];

    printf("Enter the name of the file you wish to encrypt.\n");
    scanf("%s",infile);
    printf("Enter the name of the file you wish to create.\n");
    scanf("%s",outfile);

    FILE *fin, *fout;

    if ((fin = fopen(infile, "r")) == NULL)
    {
            printf("Cannot open %s.",infile);
    }

    else if ((fout = fopen(outfile, "w")) == NULL)
    {
            printf("Cannot create %s.",outfile);
    }
    else
    {
        unsigned long c;
        unsigned long key;
            
        printf("Please enter key.\n");
        scanf("%lu",key);
        while (!feof(fin))
        {
            fscanf(fin,"%lu",&c);
            encrypt(&c,&key);
            fprintf(fout,"%lu",c);
        }
        fclose(fin);
        fclose(fout);
    }
}       

You may reconize the function encrypt() as the TEA encryption alogarithm, I didn't write it and doubt it's part of the problem.

I'm using the Dev-C++ compiler on a windows pc, if that matters.

Any help will be appreciated.
  #2  
Old 14-Sep-2005, 16:39
Lewk of Serthic Lewk of Serthic is offline
New Member
 
Join Date: Sep 2005
Posts: 5
Lewk of Serthic is on a distinguished road

Elaboration


I was using
CPP / C++ / C Code:
scanf("%lu",key);
while (!feof(fin))
and although it ran through a couple cycles, I beleive it was failing to stop on EOF.
  #3  
Old 14-Sep-2005, 17:58
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,234
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Check out this link for information on feof().
__________________

Cow: You're a lawyer too?
Mooseblood (mosquito): Ma'am, I was already a bloodsucking parasite. All I needed was a briefcase!
  #4  
Old 14-Sep-2005, 20:06
Lewk of Serthic Lewk of Serthic is offline
New Member
 
Join Date: Sep 2005
Posts: 5
Lewk of Serthic is on a distinguished road

fscanf()


Quote:
Originally Posted by WaltP
Check out http://www.gidforums.com/showpost.php?p=20674&postcount=2 for information on feof().

Can I use...
CPP / C++ / C Code:
if (fgets(buf, 20, st) == NULL)
    {
        // exit the loop when done
        break;
    }

but with fscanf() instead of fgets()?
  #5  
Old 14-Sep-2005, 20:32
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,618
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by Lewk of Serthic
I'm having some trouble with my little encryption program. It compiles alright but gives me a "Illegal Operation" when I run it, right after I input a key.


In addition to the problems with eof testing that Walt pointed out:


1.
Your scanf is wrong:

CPP / C++ / C Code:
scanf("%lu",key);

Should be something like the following to keep things legal:

CPP / C++ / C Code:
scanf("%lu",&key);

However, it won't give you a very useful encryption/decryption function for general files. What if the file does not consist of lines of ascii representations of unsigned long ints?

2.
The TEA algorithm works on 64-bit blocks of data (Can be two 32-bit ints or eight 8-bit chars). The key is a 128-bits (four 32-bit ints or 16 8-bit chars).

The function that you are using only works for the following:

v is an array of two 32-bit ints
k is an array of four 32-bit ints

3. You need to get two 32-bit ints into the key. You need to read the file a byte at a time (actually 8 bytes at a time, since the algorithm works on 64-bit blocks of data). You need to open the file in binary mode in order to be able to encrypt and decrypt and get the exact same file back again. You can pack the chars of the file into two 32-bit ints if you want to use your function (and take care of the case that the file length is not a multiple of eight).

Regards,

Dave
  #6  
Old 14-Sep-2005, 23:43
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,234
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by Lewk of Serthic
Can I use...
CPP / C++ / C Code:
if (fgets(buf, 20, st) == NULL)
    {
        // exit the loop when done
        break;
    }

but with fscanf() instead of fgets()?
You should be able to figure that out on your own. What does fscanf() return? What does fgets() return? Are they the same? If so, yes. If not, how would you have to change the if?
__________________

Cow: You're a lawyer too?
Mooseblood (mosquito): Ma'am, I was already a bloodsucking parasite. All I needed was a briefcase!
 

Recent GIDBlogPrepping for deployment by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Trouble with finding a vowel using recursion SnackMan78 CPP / C++ Forum 3 12-May-2005 14:35
Really amatuer to C++, trigonometry trouble Sancho C Programming Language 2 13-Jan-2005 03:16
C programming trouble Newworld C Programming Language 8 12-Sep-2004 23:06
Trouble with Windows XP vsseym Computer Software Forum - Windows 29 12-Aug-2004 03:56
Having trouble trying to format C: Nickster64 Computer Software Forum - Windows 2 27-Jul-2004 07:31

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:18.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.