title: C create a file path
body: <p>I have this directory tree:</p>
  
  <pre><code>xy@xy:/test# find .
  .
  ./2
  ./2/Asd
  ./2/Asd/qwe
  ./2/Asd/qwe/txt.txt
  ./1
  </code></pre>
  
  <p>There can be more directories like 4, 5, 6, whatever in the "test" folder, but asd, Asd, ASD, qwe, Qwe, QWE are constant.
  The aim is to calculate all possible paths like this:</p>
  
  <pre><code>/test/1/ASD/QWE/txt.txt
  /test/2/asd/qwe/txt.txt
  /test/2/Asd/Qwe/txt.txt
  </code></pre>
  
  <p>and after that to read the file (txt.txt)</p>
  
  <p>I created (copy &amp; pasted together...) the following and it is working for a while.
  I marked the line I have problem with.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;dirent.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;time.h&gt;
  #include &lt;stdlib.h&gt;
  
  char *filetosearch="txt.txt";
  char *test="/test/";
  char *slash="/";
  char *directory[3][3] = {{"asd", "Asd", "ASD"}, {"qwe","Qwe","QWE"}};
  int dircounter1=0;
  int dircounter2=0;
  FILE *filetosf;
  char **dirpatharr=NULL;
  int dirpathcount=0;
  char **hdarr=NULL;
  int hdarrcount=0;
  char **fullpath=NULL;
  int count,size;
  char *dirname;
  DIR  *d;
  struct dirent *dir;
  int main(void)
  {
      d = opendir(test);
      if (d)
      {
          while ((dir = readdir(d)) != NULL)
          {
              if( dir-&gt;d_type==DT_DIR )
              {
                  dirname=dir-&gt;d_name;
                  if((strcmp( dirname, ".." ) == 0) || (strcmp( dirname, "." ) == 0) )
                  {
                  }
                  else
                  {
                      hdarr=(char **)realloc(hdarr,(hdarrcount+1)*sizeof(dirname));
                      hdarr[hdarrcount]=dirname;
                      hdarrcount++;
                  }
              }
              else
              {
              }
          }
      }
  
      closedir(d);
  
      for (count=0; count&lt;hdarrcount; count++ )
      {
          for (dircounter1 = 0; dircounter1 &lt; 3; dircounter1++ )
          {
              for (dircounter2 = 0; dircounter2 &lt; 3; dircounter2++ )
              {
                  fullpath = malloc ( strlen(test) + strlen(hdarr[count]) + strlen(slash) + strlen(directory[0][dircounter2]) + strlen(slash) + strlen(directory[1][dircounter1]) + strlen(slash) + strlen(filetosearch)+1);
                  strcpy (fullpath,test);
                  strcat (fullpath,hdarr[count]);
                  strcat (fullpath,slash);
                  strcat (fullpath,directory[0][dircounter2]);
                  strcat (fullpath,slash);
                  strcat (fullpath,directory[1][dircounter1]);
                  strcat (fullpath,slash);
                  strcat (fullpath,filetosearch);
  
                  printf("fullpath:%s\n",fullpath);
                  //    filetosf = fopen(fullpath,"r");  //THIS WILL BE THE PROBLEM!!!!
  
                  if (filetosf){
                      fseek (filetosf, 0L, SEEK_END);
                      size=ftell(filetosf);
                      fclose(fullpath);
                      printf("file exist:%s\n",fullpath);
                  }
                  free (fullpath);
              }
          }
      }
  }
  </code></pre>
  
  <p>-------CUT HERE--------------------</p>
  
  <p>It has this output (like I wanted):</p>
  
  <pre><code>fullpath:/test/2/asd/qwe/txt.txt
  fullpath:/test/2/Asd/qwe/txt.txt
  fullpath:/test/2/ASD/qwe/txt.txt
  fullpath:/test/2/asd/Qwe/txt.txt
  fullpath:/test/2/Asd/Qwe/txt.txt
  fullpath:/test/2/ASD/Qwe/txt.txt
  fullpath:/test/2/asd/QWE/txt.txt
  fullpath:/test/2/Asd/QWE/txt.txt
  fullpath:/test/2/ASD/QWE/txt.txt
  fullpath:/test/1/asd/qwe/txt.txt
  fullpath:/test/1/Asd/qwe/txt.txt
  fullpath:/test/1/ASD/qwe/txt.txt
  fullpath:/test/1/asd/Qwe/txt.txt
  fullpath:/test/1/Asd/Qwe/txt.txt
  fullpath:/test/1/ASD/Qwe/txt.txt
  fullpath:/test/1/asd/QWE/txt.txt
  fullpath:/test/1/Asd/QWE/txt.txt
  fullpath:/test/1/ASD/QWE/txt.txt
  </code></pre>
  
  <p>BUT when I remove the remark sign and activate the file open part <code>filetosf = fopen(fullpath,"r");</code> 
  I am not able to read the file. When I check the output I get this:</p>
  
  <pre><code>fullpath:/test/2/asd/qwe/txt.txt
  fullpath:/test//Asd/qwe/txt.txt
  fullpath:/test//ASD/qwe/txt.txt
  fullpath:/test//asd/Qwe/txt.txt
  fullpath:/test//Asd/Qwe/txt.txt
  fullpath:/test//ASD/Qwe/txt.txt
  fullpath:/test//asd/QWE/txt.txt
  fullpath:/test//Asd/QWE/txt.txt
  fullpath:/test//ASD/QWE/txt.txt
  fullpath:/test//asd/qwe/txt.txt
  fullpath:/test//Asd/qwe/txt.txt
  fullpath:/test//ASD/qwe/txt.txt
  fullpath:/test//asd/Qwe/txt.txt
  fullpath:/test//ASD/Qwe/txt.txt
  fullpath:/test//asd/QWE/txt.txt
  fullpath:/test//Asd/QWE/txt.txt
  fullpath:/test//ASD/QWE/txt.txt
  </code></pre>
  
  <p>This even happens if I print out the lines BEFORE the <code>fopen</code> commend. Why is this happening?</p>
  
  <p>I am not an experienced programer, so please try to explain simply. </p>