problem with fprintf() output

Discussion in 'C Programming' started by Zach, Dec 5, 2010.

  1. Zach

    Zach Guest

    I am trying to have fprintf() print the string:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

    I tried:
    fprintf(fpin,"<!DOCTYPE html PUBLIC "-"//W3C//DTD XHTML 1.1//EN\n");
    fprintf(fpin,"
    http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd>\n");

    But this causes the following string to be displayed in my web
    browser:
    http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd>

    I want it to be rendered not displayed. How can I fix the error in my
    fprintf() statements?

    Regards,
    Zach
     
    Zach, Dec 5, 2010
    #1
    1. Advertising

  2. Zach

    Ben Pfaff Guest

    Zach <> writes:

    > I am trying to have fprintf() print the string:
    > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
    > "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    >
    > I tried:
    > fprintf(fpin,"<!DOCTYPE html PUBLIC "-"//W3C//DTD XHTML 1.1//EN\n");
    > fprintf(fpin,"
    > http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd>\n");


    Use \" to output a literal ", e.g.:

    fprintf(fpin,"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\"\n");
    fprintf(fpin," \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n");
    --
    "A lesson for us all: Even in trivia there are traps."
    --Eric Sosman
     
    Ben Pfaff, Dec 5, 2010
    #2
    1. Advertising

  3. Zach

    Tim Harig Guest

    On 2010-12-05, Ben Pfaff <> wrote:
    > Zach <> writes:
    >
    >> I am trying to have fprintf() print the string:
    >> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
    >> "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    >>
    >> I tried:
    >> fprintf(fpin,"<!DOCTYPE html PUBLIC "-"//W3C//DTD XHTML 1.1//EN\n");
    >> fprintf(fpin,"
    >> http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd>\n");

    >
    > Use \" to output a literal ", e.g.:
    >
    > fprintf(fpin,"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\"\n");
    > fprintf(fpin," \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n");


    Better yet, use some kind of template, in a config or resource file,
    rather then generating boilerplate xhtml from inside of your C code.
     
    Tim Harig, Dec 5, 2010
    #3
  4. Zach

    Zach Guest

    On Dec 5, 1:40 am, (Ben Pfaff) wrote:
    >
    > Use \" to output a literal ", e.g.:


    Excellent, just what I needed. Thanks!

    Zach
     
    Zach, Dec 5, 2010
    #4
  5. Zach

    Zach Guest

    On Dec 5, 2:01 am, Tim Harig <> wrote:
    >
    > Better yet, use some kind of template, in a config or resource file,
    > rather then generating boilerplate xhtml from inside of your C code.


    Something to consider :)

    Zach
     
    Zach, Dec 5, 2010
    #5
  6. Kenneth Brody <> writes:
    > On 12/5/2010 1:25 AM, Zach wrote:
    > [...]
    >> fprintf(fpin,"<!DOCTYPE html PUBLIC "-"//W3C//DTD XHTML 1.1//EN\n");
    >> fprintf(fpin,"
    >> http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd>\n");

    > [...]
    >
    > I see others have already answered, but you may be wondering why the code
    > you wrote didn't work, yet compiled.
    >
    > Consider your "string":
    >
    > "<!DOCTYPE html PUBLIC "-"//W3C//DTD XHTML 1.1//EN\n"
    >
    > This is how C sees it:
    >
    > A string literal: "<!DOCTYPE html PUBLIC "
    > A minus sign: -
    > Another string literal: "//W3C//DTD XHTML 1.1//EN\n"
    >
    > So, your "string" is actually an integer representing the difference between
    > two "char*" values. (On my system, it's -24, though it could be virtually
    > anything.)


    In fact, the behavior of the subtraction is undefined, since it
    attempts to subtract pointers to two distinct objects. As always,
    one possible result of undefined behavior is to quietly yield some
    result which may or may not make sense.

    > Now, on my system, running your first fprintf line crashes the program, as
    > fprintf() is expecting a char*, not an integer, as the second parameter. (I
    > also get a warning about this at compile time.)


    Again, passing a value of type ptrdiff_t (the signed integer type of the
    result of subtracting two pointer values) to fprintf() with a format
    that expects a char* argument results in undefined behavior. In this
    particular case, the likely result is that the value -24 (if that's what
    you happen to get) is interpreted as a char* value, possibly as
    (char*)0xffffffe8 on a 32-bit system. Since there probably isn't
    anything accessible at that address, a program crash is a very likely
    result.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Dec 6, 2010
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Arti Potnis
    Replies:
    2
    Views:
    6,695
    Howard
    Sep 2, 2004
  2. Koen
    Replies:
    1
    Views:
    585
    Victor Bazarov
    Oct 5, 2004
  3. Jos De Laender

    My error or fprintf implementation error ?

    Jos De Laender, Aug 14, 2003, in forum: C Programming
    Replies:
    7
    Views:
    526
    CBFalconer
    Aug 15, 2003
  4. Magix

    fprintf formatted output

    Magix, Jul 27, 2004, in forum: C Programming
    Replies:
    6
    Views:
    297
  5. danep2

    Strange fprintf output

    danep2, Apr 20, 2008, in forum: C++
    Replies:
    3
    Views:
    389
    danep2
    Apr 22, 2008
Loading...

Share This Page