Is that possible to write to a word file from a C language function?

廉价感情. 提交于 2021-02-07 09:25:44

问题


I have a Library management system written in C language which has I/O file in .dat. How is it possible to get output of word file from this function:

void viewbooks(void)  //show the list of book persists in library
{
    int i=0,j;
    system("cls");
    gotoxy(1,1);
    printf("*********************************Book List*****************************");
    gotoxy(2,2);
    printf(" CATEGORY     ID    BOOK NAME     AUTHOR       QTY     PRICE     RackNo ");
    j=4;
    fp=fopen("Bibek.dat","rb"); //the .dat file getting data to be showed
    while(fread(&a,sizeof(a),1,fp)==1) // .dat file to be read
    {
        gotoxy(3,j);
        printf("%s",a.cat);
        gotoxy(16,j);
        printf("%d",a.id);
        gotoxy(22,j);
        printf("%s",a.name);
        gotoxy(36,j);
        printf("%s",a.Author);
        gotoxy(50,j);
        printf("%d",a.quantity);
        gotoxy(57,j);
        printf("%.2f",a.Price);
        gotoxy(69,j);
        printf("%d",a.rackno);
        printf("\n\n");
        j++;
        i=i+a.quantity;
    }
    gotoxy(3,25);
    printf("Total Books =%d",i);
    fclose(fp);
    gotoxy(35,25);
    returnfunc();
}

回答1:


HTML is one possibility to describe rich-text. Being the file format of WWW, it is well-established. IMHO, probably any modern Rich-Text text processing tool does support it. (I personally know this for WinWord – for years.)

To write an HTML file is rather easy as the HTML file is actually nothing else than source code which may be written in plain ASCII.

A short demonstration print-HTML.c:

#include <stdio.h>

struct Entry {
  const char *author;
  const char *title;
};

void printEntry(FILE *f, struct Entry *pEntry, int i)
{
  fprintf(f,
    "<tr><!-- start of table row -->\n"
    "<td>%d</td><!-- number -->\n"
    "<td>%s</td><!-- Author -->\n"
    "<td>%s</td><!-- Title -->\n"
    "</tr><!-- end of table row -->\n",
    i, pEntry->author, pEntry->title);
}

void printTable(FILE *f, size_t nEntries, struct Entry table[])
{
  fprintf(f,
    "<table><!-- start of table -->\n"
    "<tr><!-- start of table head row -->\n"
    "<th>No.</th><th>Author</th><th>Title</th>\n"
    "</tr><!-- end of table head row -->\n");
  for (size_t i = 0; i < nEntries; ++i) {
    printEntry(f, table + i, (int)i + 1);
  }
  fprintf(f,
    "</table><!-- end of table -->\n");
}

void printDoc(
  FILE *f, const char *title, size_t nEntries, struct Entry table[])
{
  fprintf(f,
    "<!DOCTYPE html>\n"
    "<html>\n"
    "<head>\n"
    "<title>%s</title>\n"
    "</head>\n"
    "<body>\n"
    "<h1>%s</h1>\n",
    title, title);
  printTable(f, nEntries, table);
  fprintf(f,
    "</body>\n"
    "</html>\n");
}

int main()
{
  /* the sample table */
  struct Entry table[] = {
    { "Kernighan and Ritchie", "The C Programming Language" },
    { "Kernighan and Ritchie", "Programming in C" },
    { "Tim Berners-Lee", "Weaving the Web" },
    { "Tim Berners-Lee", "Hypertext Markup Language: the HTML explained from the Inventor of the WWW" }
  };
  enum { nEntries = sizeof table / sizeof table[0] };
  /* output as HTML */
  printDoc(stdout, "My Favorite Books", nEntries, table);
  /* done */
  return 0;
}

Sample session:

$ gcc -std=c11 -o print-HTML print-HTML.c

$ ./print-HTML 
<!DOCTYPE html>
<html>
<head>
<title>My Favorite Books</title>
</head>
<body>
<h1>My Favorite Books</h1>
<table><!-- start of table -->
<tr><!-- start of table head row -->
<th>No.</th><th>Author</th><th>Title</th>
</tr><!-- end of table head row -->
<tr><!-- start of table row -->
<td>1</td><!-- number -->
<td>Kernighan and Ritchie</td><!-- Author -->
<td>The C Programming Language</td><!-- Title -->
</tr><!-- end of table row -->
<tr><!-- start of table row -->
<td>2</td><!-- number -->
<td>Kernighan and Ritchie</td><!-- Author -->
<td>Programming in C</td><!-- Title -->
</tr><!-- end of table row -->
<tr><!-- start of table row -->
<td>3</td><!-- number -->
<td>Tim Berners-Lee</td><!-- Author -->
<td>Weaving the Web</td><!-- Title -->
</tr><!-- end of table row -->
<tr><!-- start of table row -->
<td>4</td><!-- number -->
<td>Tim Berners-Lee</td><!-- Author -->
<td>Hypertext Markup Language: the HTML explained from the Inventor of the WWW</td><!-- Title -->
</tr><!-- end of table row -->
</table><!-- end of table -->
</body>
</html>

$ ./print-HTML >test.html

$

Below, some snapshots of the applications I opened test.html in:

Firefox:

MS Word for Windows:

MS Excel:

Update:

In the above sample code, I carefully prevented to use meta-characters (<, >, &, and ") in the text pieces. If these characters appear in the original texts they may not be printed as is (as these characters may have special meaning in the HTML syntax). Instead, they have to be replaced by their entities:

  • <&lt; (begin of tag)
  • >&gt; (end of tag)
  • &&amp; (begin of entity)
  • "&quot; (begin/end of quoted attribute values)
  • '&apos; (alternative begin/end of quoted attribute values).

In HTML, there are a lot more pre-defined entities. (In XML, these are the only pre-defined entities.)

The updated sample code:

#include <stdio.h>

void printHTMLText(FILE *f, const char *text)
{
  for (; *text; ++text) {
    switch (*text) {
      case '<': fprintf(f, "&lt;"); break;
      case '>': fprintf(f, "&gt;"); break;
      case '&': fprintf(f, "&amp;"); break;
      case '"': fprintf(f, "&quot;"); break;
      case '\'': fprintf(f, "&apos;"); break;
      default: putc(*text, f);
    }
  }
}

struct Entry {
  const char *author;
  const char *title;
};

void printEntry(FILE *f, struct Entry *pEntry, int i)
{
  fprintf(f,
    "<tr><!-- start of table row -->\n"
    "<td>%d</td><!-- number -->\n"
    "<td>",
    i);
  printHTMLText(f, pEntry->author);
  fprintf(f,
    "</td><!-- Author -->\n"
    "<td>");
  printHTMLText(f, pEntry->title);
  fprintf(f,
    "</td><!-- Title -->\n"
    "</tr><!-- end of table row -->\n");
}

void printTable(FILE *f, size_t nEntries, struct Entry table[])
{
  fprintf(f,
    "<table><!-- start of table -->\n"
    "<tr><!-- start of table head row -->\n"
    "<th>No.</th><th>Author</th><th>Title</th>\n"
    "</tr><!-- end of table head row -->\n");
  for (size_t i = 0; i < nEntries; ++i) {
    printEntry(f, table + i, (int)i + 1);
  }
  fprintf(f,
    "</table><!-- end of table -->\n");
}

void printDoc(
  FILE *f, const char *title, size_t nEntries, struct Entry table[])
{
  fprintf(f,
    "<!DOCTYPE html>\n"
    "<html>\n"
    "<head>\n"
    "<title>");
  printHTMLText(f, title);
  fprintf(f,
    "</title>\n"
    "</head>\n"
    "<body>\n"
    "<h1>");
  printHTMLText(f, title);
  fprintf(f,
    "</h1>\n");
  printTable(f, nEntries, table);
  fprintf(f,
    "</body>\n"
    "</html>\n");
}

int main()
{
  struct Entry table[] = {
    { "Kernighan & Ritchie", "The C Programming Language" },
    { "Kernighan & Ritchie", "Programming in C" },
    { "Tim Berners-Lee", "Weaving the Web" },
    { "Tim Berners-Lee", "Hypertext Markup Language: the HTML explained from the Inventor of the WWW" }
  };
  enum { nEntries = sizeof table / sizeof table[0] };
  printDoc(stdout, "My Favorite Books", nEntries, table);
  return 0;
}

will print e.g.

{ "Kernighan & Ritchie", "The C Programming Language" }

as:

<td>Kernighan &amp; Ritchie</td><!-- Author -->
<td>The C Programming Language</td><!-- Title -->

Note:

" has actually to be replaced in double-quoted attribute values only. (as well as ' in single-quoted attribute values). In turn, < and > need not to be replaced in attribute values. To keep things simple and compact, the function printHTMLText() replaces any of these characters.



来源:https://stackoverflow.com/questions/44613163/is-that-possible-to-write-to-a-word-file-from-a-c-language-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!