Writing a file to users HOME directory in C -
i trying write .txt file user's home directory.
i have tried:
char str[] = "test"; char* file = strcat(getenv("home"), "/datanumbers.txt"); //i concatenating user's home directory , filename created. myfile = fopen(file,"w"); fwrite(str , 1 , sizeof(str) , myfile ); however, not create file.
you can't strcat() environment variable. need buffer:
char file[256]; // or whatever, think there #define this, path_max strcat(strcpy(file, getenv("home")), "/datanumbers.txt"); myfile = fopen(file,"w"); edit address 1 of comments below, should first ensure data concatenated doesn't overflow file buffer, or allocate dynamically - not forgetting free afterwards.
Comments
Post a Comment