Created
September 27, 2011 10:05
-
-
Save vasia/1244744 to your computer and use it in GitHub Desktop.
Some File Operations in C
This file contains 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
//Open file for reading/writing | |
FILE *input, *output; | |
//open file for reading | |
if((input=fopen("input", "r"))== NULL){ | |
perror("fopen"); | |
exit(EXIT_FAILURE); | |
} | |
//open file for writing | |
if((input=fopen("output", "w"))== NULL){ | |
perror("fopen"); | |
exit(EXIT_FAILURE); | |
} | |
//read-write-etc | |
close(input); close(output); | |
//-------------------------------------------// | |
//xor file data byte by byte - assumes equal file size!! | |
void xor_data(FILE* input1, FILE* input2, FILE* output){ | |
int cur_byte1, cur_byte2; | |
while(((cur_byte1 = fgetc(input1)) != EOF) && ((cur_byte2 = fgetc(input2)) != EOF)){ | |
fputc(cur_byte1 ^ cur_byte2, output); | |
} | |
} | |
//-------------------------------------------// | |
//split a file into 4 | |
#define COMMAND_SIZE 500 | |
void split_file_into4(FILE *input, char * filename){ | |
unsigned long fileSize; | |
unsigned long fileChunk; | |
char syscommand[COMMAND_SIZE]; | |
if(input){ | |
fseek(input, 0, SEEK_END); //cursor at the end of the file | |
fileSize = ftell(input); //take the length of the file | |
} | |
fileChunk = fileSize/4; | |
if (fileSize%4 != 0) | |
fileChunk++; | |
printf("The size of each file is %lu\n", fileChunk); | |
//split the files with bash command | |
sprintf(syscommand,"split --bytes=%lu --suffix-length=1 --numeric-suffixes %s chunk",fileChunk,filename); | |
system(syscommand); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment