0

So I have this task: I have a source file of, for example news website, in which there are meta tags like <meta name="author" content="Go Outside">. And, as you understand, that source file contains a lot of information. My task is to find that meta author tag and print out to the screen content of that meta tag, now it would be "Go Outside". I have no idea how to even start doing this. I had one idea to scan like 18 chars, and check if that is required meta tag, but that doesn't work as I thought:

   while(feof(src_file) == 0){
      char key[18];
      int i = 0;
      while (i < 18 && (feof(src_file) == 0)){
         key[i] = fgetc(src_file);
         printf("%c", key[i]);
         i++;
      }
      printf("\n%s", key);
   }

The problem is that it prints out rubbish at this line.

Your help would be appreciated since I have been working and studying for 10 hours straight, you might be able to save me from going mad. Thanks.

  • 1
    Your approach looks ok, besides the misuse of `feof()`, but this is another story. Perhaps you might like to show us your test input data and what your program prints out as the result, the rubbish as you call it. – alk May 25 '13 at 17:39
  • 1
    You are missing to terminate the data you read by a `0` to make it printable as a string. – alk May 25 '13 at 17:40
  • Sorry. We can start simple: lets say we have source file which looks like this: ``. So, my program outputs this: (http://s22.postimg.org/b3r71spy9/Untitled.png) –  May 25 '13 at 17:43

1 Answers1

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.

Community
  • 1
  • 1
alk
  • 69,737
  • 10
  • 105
  • 255
  • Either I did not understood you correctly, or it still prints out rubbish: (http://s10.postimg.org/v9pbofjeh/ddf.png) –  May 25 '13 at 17:49
  • 1
    @Marius: You added `"\0"` (as per your screen shot) instead of what I proposed, that is `'\0'`. Perhaps it is a good idea to take a break ... ;-) Also your mod should have made the compiler spit out a warning. Do not ignore warnings. Fix your code to make them disappear. They aren't there for nothing. – alk May 25 '13 at 17:51
  • Thanks, such a stupid mistake... Yeah, I do need a cup of coffe :) Thank you very much. –  May 25 '13 at 17:53
  • Ok. Now I have another problem. I am trying to check if it is that meta tag, so what I am doing is: `printf("key: %s\n", key); if(key == " –  May 25 '13 at 18:06
  • 1
    In C you can not use the `==` operator to compare strings, as they are nothing more then character arrays. Take a look at the `str*` family of functions. In your case `strcmp()` or also `strstr()` might help. The construct you use in your example does compare the start addresses of the two operants, which aren't the same. – alk May 25 '13 at 18:10
  • Although this is a more or less minimal change to the code given, I really can't upvote an answer which leaves `feof()` being abused as this does. This is not the correct use of `feof()`, and leaving people with any illusion that it might be OK is not OK, if you get my drift. – Jonathan Leffler May 25 '13 at 19:56
  • @JonathanLeffler: You are right, I ignored this `feof()` issue. I'll be updating my answer now ... - thanks for reminding me. – alk May 26 '13 at 07:29