-89. File Encoding

I'm a slow walker, but I never walk backwards.

題目來源: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

  • 範例 test 格式下載 here
  • 範例 test.enc 格式下載 here
  • 範例標準串流輸入 here

參考寫法如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#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;
}

Submit

Login

Testdata Set

Download Testdata