Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The reason for the garbage is that you never initialized <code>temp</code> to an empty string at the beginning of <code>main()</code>. You call <code>init(temp)</code> after processing each command.</p> <p>There are lots of other problems in your code:</p> <pre><code>orderedIds[i] = malloc(MAXCMD * sizeof(char*)); </code></pre> <p>Since <code>orderedIds[i]</code> is an array of char, not char*, you should multiply the size by <code>sizeof(char)</code>. Also, it's not clear why you're using <code>MAXCMD</code> as the size -- on the previous line this was the maximum number of words on a line, not the number of characters in a word.</p> <pre><code>strcpy(orderedIds[k], tok); </code></pre> <p>You should use <code>strncpy()</code> to ensure that you don't copy more than the size of <code>orderedIds[k]</code>.</p> <p>Another option would be <em>not</em> to preallocate all the <code>orderedIds[i]</code> in the first place. Instead of using <code>strcpy()</code>, use <code>strdup()</code> and assign this to <code>orderedIds[k]</code>; if you do this, you have to remember to <code>free()</code> all these strings.</p> <p>A third option is not to copy the strings at all. Just assign the pointers returned by <code>strtok()</code> to <code>orderedIds[k]</code>. But in this case you mustn't call <code>init(tmp)</code> until after you've forked.</p> <pre><code>strncpy(outputFile, orderedIds[numFile], strlen(orderedIds[numFile])); </code></pre> <p>The limit should be the size of <code>outputFile</code>, not the length of <code>orderedIds[numFile]</code>. <code>strncpy()</code> will never copy more than the length of the source, you need to tell it the maximum size of the destination to prevent a buffer overflow.</p> <pre><code>outputfd = open(outputFile, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU | S_IRGRP | S_IROTH); if (outputfd &lt; 0) { exit(EXIT_FAILURE); } </code></pre> <p>You should call <code>perror()</code> to report the reason that <code>open()</code> failed.</p> <pre><code>puts(strerror(errno)); </code></pre> <p>Call <code>perror()</code>, like you do elsewhere.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload