#include <stdio.h>
#include <cstdlib>
int main(int argc, char* argv[]) {
unsigned int length = 0;
unsigned int size = 64;
char* buffer = (char*)malloc(size);
FILE* file;
char c;
if (1 == argc) {
return 0;
}
file = fopen(argv[1], "rb");
if (file) {
/*
* Put all characters into a buffer.
* This will stop upon:
*
* - Reaching the end of the file
* - Hitting a carriage return character.
* - Hitting a linebreak character.
*/
while (true) {
c = fgetc(file);
if (c != EOF && c != '\r' && c != '\n') {
if (size == length) {
buffer = (char*)realloc(buffer, size += 64);
}
buffer[length++] = c;
} else {
break;
}
}
fclose(file);
/* Skip if line has length = 0 */
if (length) {
int shuffles = 50;
int swap;
/* Seed random number generator */
srand((int)&argc);
/* Shuffle */
while (shuffles--) {
for (unsigned int i = 0; i < length; i++) {
swap = rand() % length;
c = buffer[i];
buffer[i] = buffer[swap];
buffer[swap] = c;
}
}
/* Write file */
file = fopen("TextShufflerOutput.txt", "wb");
for (unsigned int i = 0; i < length; i++) {
fwrite(&(buffer[i]), 1, 1, file);
}
fclose(file);
/*
* These calls are highly unportable.
* They are only being used because this
* is meant to run *only* on Windows.
*/
system("notepad.exe TextShufflerOutput.txt");
system("del TextShufflerOutput.txt");
}
}
return 0;
}