title: Create Directory and Store File (C programming)
body: <p>I want to create a directory that has the name ends with the process ID (to make it unique) and then store the new files that I just wrote inside that directory.</p>
  
  <p><strong>What I want:</strong></p>
  
  <p>1) Create a new directory named : <em>mydirectory.1923</em> (example of the process id number)</p>
  
  <p>2) Store a file that I just created using</p>
  
  <pre><code>FILE * fPointer = fopen("new.txt",w+)
  </code></pre>
  
  <p>into  <em>mydirectory.1923</em></p>
  
  <p><strong>What I have so far is this:</strong></p>
  
  <pre><code>int bufSize = 20;
  int pid = getpid();
  char *fileName = malloc(bufSize);
  char *prefix = "that.rooms.";
  snprintf(fileName, bufSize,"%s%d", prefix, pid);
  printf("%s\n",fileName);
  struct stat st = {0};
  if (stat(fileName, &amp;st) == -1) {
      mkdir(fileName, 0755);
  }
  DIR *dir = opendir (fileName);
  if (dir != NULL) {
  
      FILE *fLib = fopen("library.txt" , "w+");
      fclose(fLib);
  
  }
  closedir(fileName);
  
  return 0;
  </code></pre>
  
  <p><strong>My Question:</strong></p>
  
  <p>This code doesn't work, apparently it says error on  the DIR part. </p>
  
  <p>Is this the right thing to do if I want to create directory, create file and store that file directly to the new directory?</p>
  
  <p>Is there any suggestion or advice to do it better than this? Thank you.</p>