是否可以从C语言函数写入word文件?

2024-04-30

我有一个用 C 语言编写的图书馆管理系统,其中有 I/O 文件.dat。如何从该函数中获取word文件的输出:

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();
}

HTML 是描述富文本的一种可能性。作为 WWW 的文件格式,它已经很成熟了。恕我直言,可能任何现代的富文本文本处理工具都支持它。 (我个人多年来就知道 WinWord 的这一点。)

编写 HTML 文件相当容易,因为 HTML 文件实际上只不过是可以用纯 ASCII 编写的源代码。

简短的演示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;
}

会话示例:

$ 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

$

下面是我打开的应用程序的一些快照test.html in:

Firefox:

Windows 版 MS Word:

微软Excel:

Update:

在上面的示例代码中,我小心翼翼地防止使用元字符(<, >, &, and ")在文本片段中。如果这些字符出现在原始文本中,则它们可能不会按原样打印(因为这些字符在 HTML 语法中可能具有特殊含义)。相反,它们必须被它们的实体所取代:

  • <&lt;(标签开始)
  • >&gt;(标签结束)
  • &&amp;(实体开始)
  • "&quot;(引用属性值的开始/结束)
  • '&apos;(带引号的属性值的替代开始/结束)。

在 HTML 中,有更多的预定义实体。 (在 XML 中,这些是唯一的预定义实体。)

更新后的示例代码:

#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;
}

将打印例如

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

as:

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

Note:

"实际上只需在双引号属性值中进行替换。 (也'在单引号属性值中)。反过来,< and >不需要在属性值中进行替换。为了使事情简单紧凑,函数printHTMLText()替换任何这些字符。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

是否可以从C语言函数写入word文件? 的相关文章

随机推荐

  • Django 模板文件夹

    我正在尝试 Django 并弄清楚如何设置urls py 以及 URL 如何工作 我已经配置了urls py在项目的根目录中 定向到我的博客和管理员 但现在我想向我的主页添加一个页面 所以在localhost 8000 所以我将以下代码添加
  • 如何在 Windows Phone 7 中创建自定义文本框?

    是否可以通过创建自定义文本框来处理 sip 我想创建一个自定义文本框 gt 创建获得焦点事件 gt 在我的自定义文本框的焦点上而不是 SIP 上 我的自定义键盘应该打开 要求 如何创建自定义文本框 打开自定义键盘而不是 SIP 获取文本字段
  • Python 终端菜单?终端着色?终端进度显示?

    我有一个广泛使用 Python 2 风格 的项目 我想知道是否有终端菜单库或类似的东西 我希望通过使用箭头键突出显示选项 一些颜色等简化一些选项 为我的脚本注入一些风味和活力 我隐约记得有一种方法可以制作 bash shell 终端菜单 但
  • Java初学者网络开发工具包/环境

    我的任务是使用 java 和 mysql 开发一个交互式网站 使用 servlet 检索和处理数据 使用小程序对客户端数据进行特殊处理 并处理客户端对不同数据视图的请求 您会推荐什么作为使用 java 进行 Web 开发的合适的通用工具包
  • DynamoDBMappingException:HASH 键没有映射

    编写 DynamoDB Java 应用程序时 如果表及其数据模型配置不正确 则在写入表或从表中检索时 您可能会收到 无 HASH 键映射 错误 完整的异常类似于 com amazonaws services dynamodbv2 datam
  • Django (JSONField) 和 tastypie

    我通过使用 JSONField 在 mysql 中创建了一个 TextField django 类型的表 这就是我的模型的样子 from django db import models from json field import JSON
  • 我什么时候应该在 ASP.NET MVC 中使用部分视图? [关闭]

    很难说出这里问的是什么 这个问题是含糊的 模糊的 不完整的 过于宽泛的或修辞性的 无法以目前的形式得到合理的回答 如需帮助澄清此问题以便重新打开 访问帮助中心 help reopen questions 我已经完成了示例 asp net m
  • 在 Tridion 2011 SP1 中实现存储扩展时,未定义名为 No bean

    我正在尝试使用下面的示例来实现存储扩展 http www sdltridionworld com articles sdltridion2011 tutorials extending content delivery storage sd
  • 错误 C2601:“main”:本地函数定义非法 - MS VS 2013 编译器

    我正在用 C 编写一个小程序 当我尝试使用 MS VS 2013 编译器编译它时 出现错误 C2601 main 本地函数定义非法 这是什么意思 我的代码是 include
  • 在新选项卡或窗口中打开链接[重复]

    这个问题在这里已经有答案了 是否可以开一个a href链接在新选项卡而不是同一选项卡中 a href http your url here html Link a 您应该添加target blank and rel noopener nor
  • 加速Cuda程序

    要更改哪一部分来加速此代码 代码到底在做什么 global void mat Matrix a Matrix b int tempData new int 2 tempData 0 threadIdx x tempData 1 blockI
  • 在 C 中实现逻辑右移

    我正在致力于仅使用按位运算符在 C 中创建逻辑右移函数 这是我所拥有的 int logical right shift int x int n int size sizeof int size of int arithmetic shift
  • 为什么嵌套 Java 类不能从 Scala 导入?

    我应该如何使用嵌套 Java 类来模拟斯卡拉莫克 特别是当所说的嵌套 Java 类来自第三方库时 鉴于以下来源 src main java Outer java Outer class that offers a Nested class
  • 如何使用 tf-idf 选择停用词? (非英语语料库)

    我已经成功评估了tf idf 函数 http en wikipedia org wiki Tf idf对于给定的语料库 如何找到每个文档的停用词和最佳词 据我所知 给定单词和文档的 tf idf 较低意味着它不是选择该文档的好单词 停用词是
  • VSTS 构建失败并显示 MSB4184 路径不是合法形式

    我正在尝试使用 VSTS 中的构建系统来构建和部署 c net Web 应用程序 我创建了一个新的单项目解决方案 因为似乎没有任何方法可以指定在多项目解决方案中构建 部署哪个项目 并设置我的构建定义以指向这个新解决方案 我已将其设置为使用
  • java.library.path 中没有字体管理器

    以下代码在我的桌面上运行得很好 BufferedImage image new BufferedImage width height BufferedImage TYPE INT RGB Graphics g image getGraphi
  • 修改 SIR 模型以包含随机性

    我正在尝试通过将真实流行曲线与随机 SIR 模型的模拟进行比较来建立一种估计传染病参数的方法 为了构建随机 SIR 模型 我使用 deSolve 包 而不是使用固定参数值 我想从以原始参数值为中心的泊松分布中绘制每个时间点方程中使用的参数值
  • If 语句不遵循其条件

    在我的滚动代码中 您只需编写 r 然后按 Enter 键 但似乎不会读取该内容并转到重新启动 while 循环的 else 让它滚动的唯一方法是输入除 r 之外的其他内容 而不是 standard in 1 解析错误 bin bash th
  • 模板编译错误 - 没有匹配的调用函数

    我正在尝试将字符串转换为数字 为此 我找到了以下方法 include
  • 是否可以从C语言函数写入word文件?

    我有一个用 C 语言编写的图书馆管理系统 其中有 I O 文件 dat 如何从该函数中获取word文件的输出 void viewbooks void show the list of book persists in library int