UPDATE
I solved it with the answer that's marked as valid, but with one slight difference. I open the file using fopen(file, "r+b")
, not fopen(file, "r+")
. The b
opens it in binary mode, and doesn't screw up the file.
I was doing a simple program which I called "fuzzer".
This is my code:
int main(int argc, char* argv[]){
// Here go some checks, such as argc being correct, etc.
// ...
// Read source file
FILE *fSource;
fSource = fopen(argv[1], "r+");
if(fSource == NULL){
cout << "Can't open file!";
return 2;
}
// Loop source file
char b;
int i = 0;
while((b = fgetc(fSource)) != EOF){
b ^= 0x13;
fseek(fSource, i++, SEEK_SET);
fwrite(&b, 1, sizeof(b), fSource);
}
fclose(fSource);
cout << "Fuzzed.";
return 0;
}
However, it doesn't work. Before, I used while(!feof)
, but it didn't work either, and I saw that it's not correct, so I changed it to (b = fgetc()) != EOF
(I suppose it's correct, right?).
When I run it, it gets stuck on an endless loop, and it doesn't modify the original file, but rather appends tildes to it (and the file quickly increases its size, until I stop it). If I change the open mode from "a+" to "r+", it simply deletes the contents of the file (but it at least doesn't get stuck in an endless loop).
Note: I understand that this isn't any kind of obfuscation or encryption. I'm not trying to encode files, just practicing with C++ and files.