C ·Î CGI ¸¦ Â¥°í Àִµ¥ Àß ¾ÈµÇ¼ Áú¹®µå¸³´Ï´Ù. ÁÖ¼Ò·Ï ÇÁ·Î±×·¥À¸·Î data.dat ÈÀÏÀ» Àоî¿Í¼ »ç¶÷À» Ãß°¡, display, save ÇØ¾ß µÇ´Âµ¥ º¯¼ö¸¦ CGI ¿¡¼ ¸ø Àд °Å °°°í main function ¿¡ ÀÖ´Â printf ¸¸ Ç¥ÇöÇϴµ¥ ³ª¸ÓÁö function ¿¡ ÀÖ´Â °Íµµ Ç¥ÇöÇÏ°í ½Í½À´Ï´Ù. µµ¿ÍÁÖ¼¼¿ä.
#include #include #include
void add(); void display(); void save();
struct nodedata
{ char ID[10]; char email[25]; char dept[8]; } person;
struct node
{ struct nodedata person; struct node *pre; struct node *rare; };
typedef struct node NODE;
NODE *head,*head1; NODE *P_Node;
void add() { struct node *tnode;
tnode=malloc(sizeof(NODE));
if (P_Node==NULL) { tnode->person=person; P_Node=tnode; P_Node->pre=NULL; P_Node->rare=NULL; }
else { P_Node->rare=tnode; tnode->pre=P_Node; tnode->rare=NULL; P_Node=tnode; P_Node->person=person; }
if(head==NULL) head=P_Node;
}
void save() { FILE *fp; head1=head;
if((fp=fopen("data.dat","r")) == NULL)
{ exit(1); }
while(head1) { if((fwrite(&head1->person,sizeof(struct nodedata),1,fp))!=1) break; head1=head1->rare; } fclose(fp); exit(1); }
void display() { int i=1;
head1=head;
printf(" display "); printf("-------------------------------------------------\n");
while(head1) { printf("%d",i++); printf(" ID : %s\n",head1->person.ID); printf(" Email address : %s\n",head1->person.email); printf(" Department : %s\n\n",head1->person.dept); head1=head1->rare; } printf("--------------------------------------------\n"); printf("displayed : %d\n",i-1);
if(!head) { printf("--------------------------------------------\n"); } }
main()
{ struct nodedata person; NODE *tnode; char sel,ch; int j;
FILE *fp;
head=NULL; P_Node=NULL;
tnode=malloc(sizeof(NODE)); tnode->pre=NULL; tnode->rare=NULL; printf("Content-type: text/html\n\n"); printf(""); printf("Add, display, save output"); printf(""); printf(" Addressbook "); if((fp=fopen("data.dat","r")) == NULL) { printf(" no input data. "); exit(1); }
while(1) { if((fread(&tnode->person,sizeof(struct nodedata),1,fp))!=1) break; if(head==NULL) { head=tnode; P_Node=tnode; }
else { tnode->pre=P_Node;
P_Node->rare=tnode; P_Node=tnode; P_Node->rare=NULL; }
tnode=malloc(sizeof(NODE));
/* P_Node->rare=tnode; tnode->pre=P_Node; tnode->rare=NULL;*/
}fclose(fp);
switch(sel) {
case '1': add(); ch=getchar();fflush(stdin); break;
case '2': display(); getchar(); fflush(stdin); break;
case '3': save(); dafault: } printf(" ID : %s ",person.ID); printf(" Email : %s ",person.email); printf(" Department : %s ",person.dept); printf(""); printf("");
} /* main end */
|