Hello. Ok, so for this program I have to read in an html file, gather all html tags ( ie: <html> <font> </i> etc.. ) and add them to a queue in which they will be printed out later. Opening the the file is easy, it's after that I'm a little stumped on. Would the most efficient way of scanning through the file and adding the tags into the queue be with fgets?

Recommended Answers

All 9 Replies

You shouldn't worry too much about efficiency yet. Just work on getting it functional. :) As for the functional part, pulling HTML tags from a file can be tricky for the general case. Probably the safest way is to walk through every character and build each tag individually before storing it. That way you can easily check to see if you're in a string or something.

Member Avatar for iamthwee

Hello. Ok, so for this program I have to read in an html file, gather all html tags ( ie: <html> <font> </i> etc.. ) and add them to a queue in which they will be printed out later. Opening the the file is easy, it's after that I'm a little stumped on. Would the most efficient way of scanning through the file and adding the tags into the queue be with fgets?

c or c++?

Sorry, it's in C.

I'm assuming fopen the file, then use fgets or getc to scan through untill "<" is found, from there store everything untill a ">" is found, them pop it in the queue. It sounds logical, I just can't figure out how to code it properly, like how to apply the "<" in the condition. I'm reading through the book to see if it describes this or shows similiar examples. It's annoying as I can't do much else without passing this step

Member Avatar for iamthwee

Do you really need a queue?

yes, regardless if it's the best way to do it, it's specified that we must use a queue and a stack. The requirements for the program were along the lines of:

"the tags read in from the file must be stored in a queue, then the stack is used to check for balancing". Technically I could do it without a queue but I'd lose a decent chunk of points for not following the guidelines.

Member Avatar for iamthwee

Ah,

What code do you have so far for creating a stack and queue in c? Or has your teacher already given you an example snippet?

>Do you really need a queue?
Well, the tags are being stored anyway, and since there's going to be a balance checking step, it makes sense to store them in the order that they were found. Thus, a queue. :)

>It sounds logical, I just can't figure out how to code it properly, like how to apply the "<" in the condition.
If you keep to that algorithm then you may discover the problems that I mentioned. However, since this is a homework project, it's unlikely that you need to write something bulletproof. The algorithm would go something like this:

while ( ( ch = fgetc ( in ) ) != EOF ) {
  /* Pull a tag */
  if ( ch == '<' ) {
    char tag[MAX];
    int i = 0;

    tag[i++] = ch; /* Save the current < */

    /* Go until the end of the array, the end of the file, or > */
    while ( i < MAX && ( ch = fgetc ( in ) ) != EOF && ch != '>' )
      tag[i++] = ch;

    /* Add the tag to the queue */
    enqueue ( tag );
  }
}
Member Avatar for iamthwee

hmm

I thought he/she wanted what was in between the tags i.e the stuff in red? :sad:

<html> here is sum crap </html>

Oh well maybe not. sorrie I gotta go now. Party time yay.

Bye.

oh no no, I need the tags themselves, everything in between and outside is ignored. ex:

<html> <title> This is a title </title> </html>

I need <html><title></title></html>

and thank you very much narue, I greatly appreciate it!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.