#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#define KEY 0xFA
char *filename = "data.dat";
int main(int argc, char *argv[]) {
FILE *fpr, *fpw;
char ch, k = (char) KEY;
//命令行参数有误
if (argc != 3 || *argv[2] != '+' && *argv[2] != '-') {
printf("Useage: execute_file filename+/-\n");
exit(0);
}
fpr = fopen(argv[1], "rb");
if (fpr == NULL) {
printf("can not open file: %s", argv[1]);
exit(0);
}
fpw = fopen(argv[1], "rb+");
if (fpw == NULL) {
printf("can not open file: %s", argv[1]);
}
while ((ch = fgetc(fpr)) != EOF) {
//0xFA
fputc(ch ^ k, fpw);
k = (*argv[2] == '+') ? ch : ch ^ k;
}
fclose(fpr);
fclose(fpw);
return 0;
}