2

I have been searching for hours and hours now and cannot figure out why this is not working. I want to create a file in C, in a directory I created, but the file is not being created. Here is the relevant code pertaining to making the directory and creating the file.

#include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<stdio.h> #include<unistd.h> #include<stdlib.h> #include<string.h> #include<time.h> #include<assert.h> char d1[50] = "myname.rooms."; char str[50]; char filename[50]; int d2 = getpid(); int dir; const char *rooms[10] = {"Lillard", "Matthews", "Batum", "Aldridge", "Lopez", "Mccollum", "Leonard", "Blake", "Gee", "Freeland"}; int z; sprintf(str, "%i", d2); strcat(d1, str); dir = mkdir(d1, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); snprintf(filename, 50, "/%s/%s.txt", d1, rooms[0]); z = strlen(filename); for(i=0; i<z; i++) { str[i] = filename[i]; } FILE *f; f = fopen("str", "w+"); fprintf(f, "ROOM NAME: %s\n", (rooms[0])); fclose(f); 

Making the directory works, there is just no file created in that directory or anywhere else. I want to loop through and create files for each of those rooms, but I just wanted to get it to function with 1 first. I printed out the str string and it came out as /myname.rooms.22222/Lillard.txt where myname.rooms.22222 is the directory and Lillard.txt should be the file name. I am stuck right now, please help.

2
  • 1
    You're not checking the return values of the system calls, so you won't ever know why your program fails. Do that. Then look at your fopen call and tell us what filename it could possibly create (and in which directory). Commented May 10, 2015 at 16:15
  • The return value of f is 0, which seems it would indicate a null pointer or a failure to create or open the file. Commented May 10, 2015 at 16:56

2 Answers 2

2

You are creating the file with literal name "str" in the current directory where you run the program.

Do something like this:

char dir_name[PATH_MAX]; mode_t dir_mode = ...; /* your file mode bits */ int i = ...; /* your value */ snprintf( dir_name, PATH_MAX, "whatever.%d", i ); if ( mkdir( dir_name, dir_mode ) == -1 ) { if ( errno != EEXIST ) err( 1, "mkdir" ); } if ( chdir( dir_name ) == -1 ) err( 1, "chdir" ); char file_name[FILENAME_MAX]; snprintf( file_name, FILENAME_MAX, "%s.txt", whatever ); FILE* fp = fopen( file_name, "w+" ); if ( fp == NULL ) err( 1, "fopen" ); /* now you have the file open for writing */ ... fclose( fp ); 
Sign up to request clarification or add additional context in comments.

5 Comments

Sorry, that happened in the process of trying out a lot of things to troubleshoot. If I take off the quotes I get a seg fault. After checking the return value of f it returns 0.
You don't need that str variable at all, just use the string with the formatted file name.
I tried just using my filename value in there as well and it still seg faulted. I will try out the code you provided.
I tried this out and it keeps saying fopen: No such file or directory. I know the directory exists because I can find it and open it in Linux, so there is obviously something going wrong with the file creation.
Nevermind, I was able to get it to work. Thank you very much for your help.
0

In this code the file should be called "str" (is this intended for testing or did you actually want to use contents of str variable?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.