lundi 26 octobre 2015

Append Random Text to File (C)

I'm trying to create a file inside a directory, then append some random text inside of the file.

My Code

char dirname[30]; 
sprintf(dirname, "myroom.%d", (int)getpid()); 
mkdir(dirname,0777); 

char path[path_max+1];
snprintf(path1, PATH_MAX+1, "%s/file1.txt,dirname); 
FILE *filedir1 = fopen(path1, "a+"); 
fclose(filedir1); 

char *random_name = { "burger", "toast", "burrito", "noodles" };
int number = rand();
fputs(random_name[number], filedir1];

What I want

(Inside directory "dirname")

When I open file1.txt, I expect there will be either the word burrito, burger, toast, or noodles in the first line.

What I get

file1.txt still empty.

Questions

Anybody know what happen with my code? I saw from youtube video, to append some text into a file, all I need is the fputs command but it doesn't seem to work in my code. Is it because I'm using "a+" in fopen?

Any help will be highly appreciated. Thanks




Aucun commentaire:

Enregistrer un commentaire