Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > problem with fprintf() output

Reply
Thread Tools

problem with fprintf() output

 
 
Zach
Guest
Posts: n/a
 
      12-05-2010
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
 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      12-05-2010
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
 
Reply With Quote
 
 
 
 
Tim Harig
Guest
Posts: n/a
 
      12-05-2010
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.
 
Reply With Quote
 
Zach
Guest
Posts: n/a
 
      12-05-2010
On Dec 5, 1:40*am, b...@cs.stanford.edu (Ben Pfaff) wrote:
>
> Use \" to output a literal ", e.g.:


Excellent, just what I needed. Thanks!

Zach
 
Reply With Quote
 
Zach
Guest
Posts: n/a
 
      12-05-2010
On Dec 5, 2:01*am, Tim Harig <user...@ilthio.net> 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

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-06-2010
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) kst- <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"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange problem - page output contains output from another request Paul ASP .Net 1 04-10-2007 03:41 PM
parse output screen ok but cant get desired output new file! chuck amadi Python 1 06-23-2004 02:16 PM
Sony Precision Cinema Progressive Output vs Component 480p Output Otto Pylot DVD Video 1 04-18-2004 09:49 PM
Is Fuji S3000 3.2m/pixel output, or 6 m/pixel interpolated output? Peter H Digital Photography 43 12-04-2003 02:35 PM
Output / Debug window output bug? John Bentley ASP .Net 0 09-10-2003 07:38 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57