Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Each time you call fgetc() you advance the internal file pointer one character further. At the end of your while() loop the file pointer will then be at the end of the file. Subsequent calls intented to read on the file handle will fail with an EOF condition.</p> <p>The fgets manual says that :</p> <blockquote> <blockquote> <p>If the end-of-file is encountered while attempting to read a character, the eof indicator is set (feof). If this happens before any characters could be read, the pointer returned is a null pointer (and the contents of str remain unchanged).</p> </blockquote> </blockquote> <p>The consequence is that tmp_str is left unchanged. The garbage you get back when you call printf is actually a part of the conc() function stack.</p> <p>A solution to your problem would be rewinding the file pointer with fseek() just before calling fgets().</p> <pre><code>fseek(source, 0, SEEK_SET); </code></pre> <p>Then a better way to get the size of your file would be to fseek to the end of the file, and use ftell to get the current position :</p> <pre><code>long size; fseek(source, 0, SEEK_END); size = ftell(source); </code></pre> <p>This being said, your code still has a problem. When you allocate on the stack (variables local to a function) you have to tell the size of the variable at compile time. Here your compiler allocates a char array of length 0. I suggest you investigate dynamic allocation with <em>malloc</em> of the keyword <em>new</em> if you're coding in C++.</p> <p>A proper allocation would look like this :</p> <pre><code>char *tmp_str = malloc(size); // Here you read the file free(tmp_str); </code></pre> <p>An simpler solution could be to preallocate a string large enough to hold your file.</p> <pre><code>char tmp_str[1024 * 100]; // 100Kb </code></pre> <p>Then use the <em>size</em> variable we got earlier to check the file will fit in tmp_str before reading.</p>
    singulars
    1. This table or related slice is empty.
    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. This table or related slice is empty.
    1. This table or related slice is empty.
    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