c - Creating a File of Random Size [1...500] KB -


foreword: have simplified problem key functionalities, if sounds weird because small aspect of whole program.

problem:

i want create 100 text files: i'll loop , use loop counter name files.

then, want populate each file random strings. use string struck defined below this. want fill file [1kb 500kb].

struct string // , yes using own string library. {    char *c;    int length;    int maxlength; } 

lets assume have file opened (probably @ moment create it, empty). check this.

int range = random.range(0,500); 

i number predetermine file size. if range == 100 file populated 100kb of "data".

i first have string created.

// maybe making 100 chars help? string *s1 = makestring("abcdefghijklmnopqrstuvwxyz");  

how figure out how many times have write string s1 file make size of range? preferably before writing file, wouldn't want write first check, write again.

and how random integer value in c? used random.range in c#.

to keep simple, best if can make string size common denominator of 1kb (1024 bytes). don't have take care fraction. after can @naitoon mentioned above (range*1024)/s1->length. if each of character of string 1 byte long.

as random integer, can call standard library rand() returns integer between 0 rand_max, @ least 32767.

also, in order keep random number range(0~500), can modular of return value.

    range = rand() % 500; 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -