lundi 2 novembre 2020

(C) generating random data using /dev/urandom and writing it into file

I'm pretty new to C, and I'm not sure what is wrong with this piece of code I have written. It is supposed to open (or create if needed) a file using a name specified and in that file write a bunch of signs taken from the /dev/urandom file. I need a precise amount of elements each one of a specified length. I need to be able to later edit that created file, but I wanted to focus on creating this generator part first. It doesn't show any errors when compiling or when I try to execute it, but still nothing happens - the file still doesn't exist anywhere. What's wrong with it?

int main(){

     void generate(char str[], int a, int b);

}


void generate(char str[], int a, int b){
    int n=0;
    char fname[128];
    strcpy(fname,str);
    strcat(fname, ".txt");
    FILE *myFile = fopen(fname, "w");
    FILE *randomiser = fopen("/dev/urandom", "r");
   
    char bufor[a];
    size_t result = fread (bufor, b, a, randomiser);
    size_t end = fwrite (bufor, b, a, myFile);
   
    fclose(myFile);
    fclose(randomiser);
}

@edit change the tile as someone suggested and changed a code a bit since im still trying to work it out I forgot to mention that the whole point of this function is for it to be called in terminal as for example ./program generate data 100 100.




Aucun commentaire:

Enregistrer un commentaire