problem with fprintf() output

Z

Zach

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
 
B

Ben Pfaff

Zach said:
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");
 
Z

Zach

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
 
K

Keith Thompson

Kenneth Brody said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top