C Programming Task, Html Source File
So I have this task: I have a source file of, for example news website, in which there are meta tags like . And, as you understand, t
Solution 1:
You are missing to zero-terminate the char
-array to enable it to be handle as a string before printing it.
Mod you code either like so:
...
{
char key[18 + 1]; /* add one for the zero-termination */
memset(key, 0, sizeof(key)); /* zero out the whole array, so there is no need to add any zero-terminator in any case */
...
or like so:
...
{
char key[18 + 1]; /* add one for the zero-termination */
... /* read here */
key[18] = '\0'; /* set zero terminator */
printf("\n%s", key);
...
Update:
As mentioned in my comment to your question there is "another story" related to the way feof()
is used, which is wrong.
Please see that the read loop is ended only after an EOF had been already been read in case of an error or a real end-of-file. This EOF pseudo character, then is added to the character array holdling the reads' result.
You might like to use the following construct to read:
{
int c = 0;
do
{
char key[18 + 1];
memset(key, 0, sizeof(key));
size_t i = 0;
while ((i < 18) && (EOF != (c = fgetc(src_file))))
{
key[i] = c;
printf("%c", key[i]);
i++;
}
printf("\n%s\n", key);
} while (EOF != c);
}
/* Arriving here means fgetc() returned EOF. As this can either mean end-of-file was
reached **or** an error occurred, ferror() is called to find out what happend: */
if (ferror(src_file))
{
fprintf(stderr, "fgetc() failed.\n");
}
For a detailed discussion on this you might like to read this question and its answers.
Post a Comment for "C Programming Task, Html Source File"