tags:

views:

83

answers:

4

I want to encrypt and decrypt 10000 bytes of a music file. I used the code :

if(file!=NULL && g!= NULL)  
{
    while(!feof(file))
    {
        count++;
        char aByte;
        fread(&aByte, sizeof(char), 1, file);
        if (count <= 10000)
        {
            if (count % 2 == 0)
            {
                if ((aByte ^ 'A') != EOF) aByte = aByte ^ 'A';
            }
            else 
            {
                if ((aByte ^ 'B') != EOF) aByte = aByte ^ 'B';
            }
        }
        fwrite(&aByte, sizeof(char), 1, g);
    }
}   
fclose(file);

But the code does not work. Please help!!!

+2  A: 
David
Sr. I'm not understand. But i check for EOF because i don't want to write EOF to file. If the byte xor with key makes a result EOF i will write the byte to file.
I would just remove the `feof()` call and instead check the return value of `fread` each time you call it. If it returns zero, there's no more data, so you break out of the loop.
David
while(!feof(file)). SR this checking is not enough?
It would be enough if you were using it correctly, but in your particular case it'd be better just to check the return value of `fread`. That way, you don't have to go `if (aByte != EOF)`. (Which won't work, in any case, since `aByte` would have to be an integer.)
David
To clarify the above comment: Your problem seems to be that you're mixing different (valid) ways of reading bytes in C. You should just pick one -- maybe look at some example code and only add the XOR stuff once you've got it to read and write characters properly.
David
Sr i don't check aByte != EOF, i checked : aByte ^ 'A' != EOF. I think this way to avoid meet the feof() and write it to file.
I know, and I've already explain why **that** isn't right. Try editing your post to update your code based on the suggestions already posted, and then we'll see if it's better.
David
EOF is not a char, you can't possibly write it to the file. aByte cannot possibly be EOF. You need to check the result of fread.
Lou Franco
A: 

Sr. I'm not understand. But i check for EOF because i don't want to write EOF to file. If the byte xor with key makes a result EOF i will write the byte to file

EOF isn't a byte in the file, it is a result of calling an API.
EJP
I check the value of EOF and i can see it seem equals -1 #define EOF (-1)
You are forcing a cast. -1 in a file is not EOF. EOF is a special int returned by some file access functions (not fread) to indicate that you can't cast the return to a char. Read the docs for fread -- it doesn't use it. You are using the conventions for a different function. Every answer here is telling you that -- if you don't trust us, read the docs.If fread encounters and end-of-file, it returns less than the number requested (in your case 0). You then check to see if this means end-of-file by calling feof() (it will return less on errors too). There is no EOF character.
Lou Franco
A: 

In this case. It is so simple with me. I don't need to encrypt a file in a high level.

A: 

Test your file handling by removing the "encryption". Take out these lines

        if (count % 2 == 0)
        {
            if ((aByte ^ 'A') != EOF) aByte = aByte ^ 'A';
        }
        else 
        {
            if ((aByte ^ 'B') != EOF) aByte = aByte ^ 'B';
        }

Is your file the exact same? I think it will not be. Here are some reasons

  1. The file is longer than 10,000 bytes -- you close file at the end, so I assume that those bytes are lost
  2. The file is less than 10,000 bytes -- you don't check for EOF property, so it looks like an extra byte will be written
  3. count is not initialized in this code -- if it's odd on one run and even on another, it wont' match. Also, it's just going to copy a random number of bytes depending on what count is. Perhaps it's initialized elsewhere.
  4. You don't close g, so you can't be sure it was flushed (fwrite can buffer writes)

UPDATE: EOF is not a byte that you can find in a file, it's a special return value used by some FILE API functions (not fread).

The function getchar() is defined like this:

  int getchar();

It either returns EOF or an int that is within the range of char. If it's EOF, then that means that you are at the end of the file. If not, you may safely cast the return to a char. The only stipulation on EOF is that it isn't a valid char. getchar() returns an int because it can return any char (256 choices) + EOF for a total of 257 choices which can't be represented in a char.

If you look at fread(), you will see that it doesn't have anything to do with chars. It takes a buffer, and record size, and a record count and returns the number of bytes read. You happen to be passing in sizeof(char) and 1 to get a char. It couldn't possibly store EOF in that char because EOF is not a valid char, it's an int and won't fit. It doesn't use the same conventions as getchar(). If it encounters EOF, it returns a number less than the total number of bytes requested. In your case, that's 0. If fread() returns 0, check feof() and ferror() to find out if you got to the end-of-file or got an error (both cause fread to return less than the number requested).

Lou Franco