I'm not talking about huge project but short little pieces of code you'd be willing to post. I'm just interested to see what quick little things other people come up with. For me, my favorite is this one. It is in C.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
FILE* getFile(char* prompt, char* fileType)
{
char* name = malloc(256*sizeof(char));
printf("%s\t", prompt);
scanf("%256s", name);
FILE* file = fopen(name, fileType);
free(name);//this solves the memory leak doesn't it?
return (file == NULL) ? getFile(prompt, fileType) : file;
}
void encrypt(FILE* enc, FILE* key, FILE* ofs)
{
printf("beginning file encryption\n");
char c, k;
while(!feof(enc))
{
if(feof(key))rewind(key);
c = getc(enc);
k = getc(key);
int put = (int)(c ^ k);
putc(put, ofs);
}
printf("\nencryption completed\n");
}
int main()
{
FILE* enc = getFile("Input file to encrypt/decrypt:", "rb");
FILE* key = getFile("Input file to use as encryption key:", "rb");
FILE* ofs = getFile("Input location to output encrypted file:", "wb+");
encrypt(enc, key, ofs);
getchar();//clears buffer
printf("press any key to exit");
getchar();
return 0;
}
The idea was to encrypt one file using another file in such a way that the method used for encryption could also be used for decryption.
I was hoping other people would post some of there pet code and explain what it does.