Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* This program's generates random numbers and write them out to a file
- Then get the md5sum of the file.
- */
- /* EDITORS NOTE:
- Finally had time to check this out this morning.
- I made quite a few assumptions in this program for the sake of brevity.
- It assumes a few things, like for example that the numbers generated
- are 32bit signed ints or less. Since they come from rand() this should always
- be the case, but the 'number' requirement was non-specific so I just chose
- the simplest type.
- There's also next to no error/input handling, and assumptions are made as to
- os, md5sum being installed, and the ability to write in the local directory.
- I did not see portability requirements specified, so I chose the path of
- least resistance on all of these fronts.
- I left the original code intact, and tried to document my thoughts when
- rewriting this. C isn't as scary as it initially seemed, I didn't have to
- use malloc a single time :)
- Compiled with: gcc source.c -g -Wall -O3 -o gen
- Generated a million lines in half a second on the cheap vps I wrote it on, so
- I think performance is ok.
- */
- //#include "/usr/include/stdio.h"
- #include <stdio.h> // I think the compiler knows where to find this one lol
- #include <stdlib.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <string.h>
- #include <string.h>
- #include <time.h>
- //void get_random_number(void *random){ //i'd rather avoid pointers thank you
- int get_random_number(void) {
- int r = rand(); // not even random in the original implementation
- //random = r; // see above
- return r;
- }
- /* See comments on line 93, and please stop putting tabs everywhere.
- char *getStringFromInt(long i, char**string)
- {
- char str[8];
- sprintf(str, "%-d", i);
- *string = str;
- return str;
- }
- */
- /* no thanks
- void write_string_to_file(char *random_number)
- {
- int fd = open("output.txt", O_WRONLY | O_APPEND, 0777);
- write(fd, random_number, strlen(random_number));
- return;
- }
- /* no thanks
- void printRandomNumber(char *file)
- {
- char *command = malloc(32);
- sprintf(command, "md5sum %s", file);
- system(command);
- free(command);
- }
- */
- int main(int argc, char** argv)
- {
- srand(time(NULL)); // make rand() actually pseudo-random
- FILE * fptr; // pointer for future filesystem interraction
- printf("About to generate ");
- printf(argv[1]);
- printf(" random numbers...\n");
- printf("Press 'y' to continue.. ");
- char answer[2]; // limited to 1 character for y/n, 2nd character null byte,
- //gets(answer); // 1. why are using the y/n variable 2. why are we using gets,
- // perhaps this is a memory exploration program
- //printf("answer: %s %p\n", answer, answer); // Why are we printing a pointer?
- // plus, answer isnt even set yet
- fgets(answer, 2, stdin); // accepts 1 character string, 2nd character is \0
- printf("answer: %s\n", answer);
- char number[11]; // Up to 10 characters accepted as input for amount to generate
- // 32bit signed int max value is 10 characters anyway
- //if(answer[0] == "y"){ // 'y' usually means the user wants to continue, not abort
- if (answer[0] != 'Y' && answer[0] != 'y' && answer[0] != '\n') { // also count newline because entering y is annoying
- printf("You answered %s, aborting\n",answer);
- return 0;
- }
- //short count = atoi(argv[1]); // limiting ourselves to 65k, 2.1b sounds like a better limit
- int count = atoi(argv[1]);
- fptr = fopen("numgen.txt", "w");
- if (fptr == NULL) {
- printf("Cannot open file \"numgen.txt\", aborting.\n");
- return 0;
- }
- //while(--count){ // off by 1 error, 1 short
- for(int i=0; i<count; i++) {
- //int *a = malloc(4); // i'd like to avoid malloc when possible
- int a = get_random_number(); // this is limited to 32bit signed ints for simplicity
- // it could be a different datatype if numbers > 2.1b are needed
- //get_random_number(a); // I like my approach above more
- //char *str;
- //getStringFromInt(a, &str); // didn't even read this function, I definitely don't want to have an
- // entire function declared which only does this
- char buffer[11]; // assuming 32 bit int again, 1 for terminator
- snprintf(buffer, 10, "%d", a); // assuming we want a base 10 int of course :^)
- fprintf(fptr, "%s\n", buffer); // writes numbers to new file, numgen.txt, every time
- // default perms, def not 777 like other function LOL
- //write_string_to_file(str); // why do we need a function for this, plus file only needs to be opened once
- //printRandomNumber("output.txt");
- }
- fclose(fptr);
- system("md5sum numgen.txt\n"); // This isn't portable but realsticically it's what I'd use
- // If I am supposed to make it portable, I could, but I'd rather just save
- // time and complexity by assuming this is running on a linux system w/ md5sum
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement