开发者

How to add formatting data to text output from c program?

开发者 https://www.devze.com 2023-02-18 15:27 出处:网络
I have a command-line C program (DOS/Windows) which produces a hex dump of several records as a .txt file.

I have a command-line C program (DOS/Windows) which produces a hex dump of several records as a .txt file. I am looking for a way to emphasize certain values without resorting to a开发者_JS百科scii-art.

Is there a combination of readily available viewing programs and byte sequences that would let me highlight/bold/underline certain blocks of text? Bonus points if I can generate it with a simple fprintf or fwrite call.

I guess HTML is an option, but I was hoping for something even simpler, something along the lines of ANSI escape codes.

Any ideas?


And if HTML is the best, what is the minimal header/tail I need to produce a valid document?

(dammit Jim, I'm an embedded programmer, not a web designer)


I work predominately on Linux/Unix systems, so I don't know what kind of support recent versions of the Windows/DOS CMD shell have for ANSI escape codes. I agree that HTML is probably your best bet.

Minimal HTML document per W3C

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  "http://www.w3.org/TR/html4/strict.dtd">

<html>
  <head>
     <title>An HTML document</title>
  </head>
  <body>
    Body of HTML document
  </body>
</html>

You could highlight specific elements using either italics (<i> or <em>) or bold (<b> or <strong>).


If you don't like using HTMl you may use the rtf format. You can generate it using the fprintf or fwrite call. Check this link in wikipedia it contains an example of rtf http://en.wikipedia.org/wiki/Rich_Text_Format


Maybe a little bit overkill, but I sometimes use XML+XSLT for this purpose. Just surround your data blocks with some tags you choose, like :

<blocktype1>...</blocktype1>
<blocktype2>...</blocktype2>

And then create a simple XSL stylesheet that will convert your dumped data to html.

Benefits :

  • Does not spam your C code with formatting specific code
  • Allows to change the formatting without running your original code
  • Allows to browse the resulting file with tools like XMLNotepad (free) or XmlSpy
0

精彩评论

暂无评论...
验证码 换一张
取 消