題目來源:judgegirl from ntu prof. pangfeng Liu
Task Description
Write a program to encode a file. You will be given a key from standard in between and , and you need to read every byte from the file test, then exclusive or it with the key, then write the result to file test.enc. Please read and write files in binary mode.
Sample Input File and key
63
(The filename will always be test)
Sample Output File
(In file test.enc)
Hint
參考寫法如下:
123456789101112131415161718192021 #include <stdio.h>
#include <stdlib.h>
#define MAXN 65536
int
main() {
FILE
fin =
fopen
(
"test"
,
"rb"
);
FILE
fout =
fopen
(
"test.enc"
,
"wb"
);
int
key, size_n;
char
buf[MAXN];
scanf
(
"%d"
, &key);
while
((size_n =
fread
(buf, 1, MAXN, fin)) != 0) {
for
(
int
i = 0; i < size_n; i++)
buf[i] ^= key;
fwrite
(buf,
sizeof
(
char
), size_n, fout);
}
fclose
(fout);
fclose
(fin);
return
0;
}