2017-06-18 77 views
1

我有用C语言编写的图书馆管理系统,其中有I/O文件.dat。如何从这个函数得到word文件的输出:可以从C语言函数写入一个字文件吗?

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

from * this * function?好吧,根本不是。但是,当然你可以编写文件,掌握OOXML规范并做相应的输出(提示:这是一个**大量的工作,你的“功能”可能会增加到几个模块) –

+2

什么是' a',为什么你期望每个数据元素都具有相同的确切大小(包括终止零)?这与编写一个Word文件有什么关系? –

+0

@Felix Palmen你会给我一些链接要遵循吗? – moh89

回答

6

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

Snapshot of <code>test.html</code> in Firefox

MS Word for Windows中:

Snapshot of <code>test.html</code> in MS Word for Windows

MS Excel中:

Snapshot of <code>test.html</code> in MS Excel

更新:

在上面的示例代码,我仔细防止使用元字符(<,>,&")。如果这些字符出现在原始文本中,则它们可能不会按原样打印(因为这些字符在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" } 

为:

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

注:

"实际上是在只有双引号中的属性值替换。 (以及单引号属性值中的')。反过来,<>不需要在属性值中被替换。为了使事情简单而紧凑,功能printHTMLText()取代了这些字符中的任何一个。

+0

可以帮助将.dat文件格式化为任何其他有用的格式来读取,但问题是你已经定义了常量变量!我的变量都是类的对象,那么如何转换它们?! – moh89

+0

为了多走一英里而竖起大拇指。 – Shark

+0

@ moh89我定义了常量变量来使样本紧凑。在示例代码中,您只是提供了从.dat文件读取数据并将其打印格式化的代码。在你的示例代码中,你用'fread()'读了'a',但是'a'(可能是'struct')的声明丢失了。因此,我声明了'struct Entry' - 只是为了展示这个概念。我认为你将能够适当地将它应用到你的项目中。 – Scheff

相关问题