Created
August 2, 2016 18:23
-
-
Save hasherezade/f28e458dcb5e8e23209e141feaf9334f to your computer and use it in GitHub Desktop.
Reverse file content
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| size_t filesize(FILE *f) | |
| { | |
| fseek(f, 0, SEEK_END); | |
| size_t size = ftell(f); | |
| fseek(f, 0, SEEK_SET); | |
| return size; | |
| } | |
| int main(int argc, char *argv[]) | |
| { | |
| if (argc < 3) { | |
| printf("Requited arguments: <input_file> <output_file>\n"); | |
| return -1; | |
| } | |
| char *inname = argv[1]; | |
| char *outname = argv[2]; | |
| FILE *fin = fopen(inname,"rb"); | |
| if (fin == NULL) { | |
| printf("[ERROR] Cannot input output file\n"); | |
| return -1; | |
| } | |
| size_t fsize = filesize(fin); | |
| printf("file size: %x\n",fsize); | |
| unsigned char buffer[fsize]; | |
| fread(buffer, 1, fsize, fin); | |
| fclose(fin); | |
| fin = NULL; | |
| FILE *fout = fopen(outname, "wb"); | |
| if (fout == NULL) { | |
| printf("[ERROR] Cannot open output file\n"); | |
| return -1; | |
| } | |
| const size_t lastchar = fsize - 1; | |
| for (int i = 0; i <= fsize/2; i++) { | |
| unsigned char swapc = buffer[i]; | |
| buffer[i] = buffer[lastchar - i]; | |
| buffer[lastchar - i] = swapc; | |
| } | |
| fwrite(buffer, 1, fsize, fout); | |
| fclose(fout); | |
| printf("File %s reversed and saved to: %s\n", inname, outname); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment