Thread: HTML to TXT

  1. #31
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    fgets(filename, sizeof(filename), stdin);
    filename[strlen(filename)-1]='\0';
    fgets will put the \n character in the buffer only if there is enough space...
    So better search for the presence of the \n character and not replace the last one without check. (see example in th eFAQ)

    Also 100 bytes buffer maybe not long enough to store the file name with path
    In windows MAX_PATH constant is at least 256 characters
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  2. #32
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    [edit]
    Also 100 bytes buffer maybe not long enough to store the file name with path
    In windows MAX_PATH constant is at least 256 characters
    But if MAX_PATH is anything like FILENAME_MAX, you shouldn't declare an array of that size. FILENAME_MAX is often 2^16-1 = 32767 on Linux systems or higher. I'd use BUFSIZ myself, which is at minumum 512 and often 8192 for 64-bit systems. [/edit]

    Quote Originally Posted by eobergfell View Post
    Code:
    #include <stdio.h>
    int main(argc, argv)
    int argc; char *argv;
    {
      char inputchar;
      int count = 0;
      char filename[100];
            FILE *open(), *fp;
            printf("Enter Filename: ");
            fgets(filename, sizeof(filename), stdin);
            filename[strlen(filename)-1]='\0';
            fp = fopen(filename, "r");
      while ( ( inputchar= fgetc(stdin) )!=EOF )
      {  if ( inputchar != '<' && count == 0 )
         { fputc( inputchar,stdout );
         }
         else
         if ( inputchar == '<' )
         { count++;
         }
         else
         if (inputchar == '>' )
         { count--;
         }
    
      }
    }
    With the help of an engineer I got this so far.
    You shouldn't declare main() like that. That's the old K&R style. You should use
    Code:
    int main(int argc, char *argv[]) {
    At any rate, argv should be declared as **argv or *argv[], not *argv. It's an array of strings, not a single pointer.

    There are also several other problems with your code. The most obvious is that HTML is never nested -- you don't get <html <something > >. So you only need to have count as 1 or 0, not zero or more -- though it works as you have it, it's just overkill.

    Also, inputchar must be an int to store EOF, like I think I said earlier. You can't store EOF in a char (properly).

    Code:
    FILE *open(), *fp;
    You're still prototyping a function here. Believe me, that's not what you want to do. Get rid of the ()s if you want to use the variable open, or get rid of it entirely if you don't intend to use it.

    And your original problem stated that you had to read from a file. You're opening a file, but you're reading from stdin (the keyboard, usually). You might also want to open another file for writing and print to that instead of to stdout (the screen, usually).

    You're also comparing inputchar against '<' twice. The logic would make more sense when written like this:
    Code:
    if(c == '<') count ++;
    if(!count) putchar(c);
    if(c == '>') count --;
    The putchar() goes in between the if statements to avoid printing '<' or '>'.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please Help - C code creates dynamic HTML
    By Christie2008 in forum C Programming
    Replies: 19
    Last Post: 04-02-2008, 07:36 PM
  2. Writing an HTML Preprocessor
    By thetinman in forum C++ Programming
    Replies: 1
    Last Post: 09-17-2007, 08:01 AM
  3. Parsing HTML files
    By slcjoey in forum C++ Programming
    Replies: 2
    Last Post: 08-28-2005, 07:01 AM
  4. Design + HTML
    By orbitz in forum C Programming
    Replies: 8
    Last Post: 11-21-2002, 06:32 AM