实现代码一:
#include "stdio.h" #include "malloc.h" #include "string.h" #include "stdlib.h" #define N 28 /*每个成员的最大长度*/ typedef struct node { char name[N]; char value[N]; struct node *next; }node; void print(struct node *head) { struct node *p; p=head; if(p == NULL) { printf("Don't find the file !\n"); } while(p != NULL) { printf("%s %s\n",p->name,p->value); p=p->next; } } void save(char*filename,struct node *head) { FILE* fp; node *p; int flag=1,count=0; /*为输出打开一个二进制的文件*/ fp = fopen(filename,"w"); if(fp == NULL) { printf("\n=====>提示:重新打开文件时发生错误!\n"); exit(1); } /*将p连接在head的后面*/ // p=head->next; //关键是有头指针,还是没有头指针 p = head; /*判断p是否为空,以便进行循环*/ while(p) { /*把p中的内容写到fp中*/ if(fwrite(p,sizeof(struct node)-4,1,fp)==1) { p=p->next; /*p向后移动一*/ count++;/*用于统计保存书籍记录的数目*/ } else/*如果fwrite(p,sizeof(struct book),1,fp)返回值不是一,提前跳出循环*/ { flag=0; break; } } if(flag==1) { printf("\n=====>提示:文件保存成功.(有%d条记录已经保存).\n",count); } fclose(fp); //关闭fp文件 } int main(int argc, char* argv[]) { struct node *head,*p[9]; head = NULL; char *filename = "D://tianmo.txt"; /*把修改的配置文件保存*/ char * ip_addr = "1.1.1.1"; char *netmask_addr = "2.2.2.2"; char *brdaddr_addr = "3.3.3.3"; char *mac_addr = "4.4.4.4"; int i; for(i= 0;i < 9 ;i ++){ p[i] = (node *)malloc(sizeof(node)); } head = p[0]; memset(p[0]->name,0,N); strcpy(p[0]->name , "# Advanced Micro Devices[AM");//28 p[0]->name[27] = '\0'; memset(p[0]->value,0,N); strcpy(p[0]->value , "D] 79c970 [PCnet32 LANCE \0");//28 p[0]->value[27] = '\0'; memset(p[1]->name,0,N); strcpy(p[1]->name , "\nDEVICE= \0"); p[1]->name[27] = '\0'; memset(p[1]->value,0,N); strcpy(p[1]->value, "eth0 \0 "); p[1]->value[27] = '\0'; strcpy(p[2]->name , "\nBOOTPROT= "); p[2]->name[27] = '\0'; strcpy(p[2]->value , "static "); p[2]->value[27] = '\0'; strcpy(p[7]->name , "\nONBOOT= "); p[7]->name[27] = '\0'; strcpy(p[7]->value, "yes "); p[7]->value[27] = '\0'; strcpy(p[8]->name , "\nTYPE= "); p[8]->name[27] = '\0'; strcpy(p[8]->value, "Ethernet "); p[8]->value[27] = '\0'; memset(p[3]->name,0,N); strcpy(p[3]->name , "\nIPADDR="); p[3]->name[8] = '\0'; memset(p[3]->value,0,N); for( i = 0;ip_addr[i] != '\0';i++) p[3]->value[i] = ip_addr[i]; p[3]->value[i+1] = '\0'; memset(p[4]->name,0,N); strcpy(p[4]->name ,"\nNETMASK="); p[4]->name[9] = '\0'; memset(p[4]->value,0,N); for( i = 0;netmask_addr[i] != '\0';i++) p[4]->value[i] = netmask_addr[i]; p[4]->value[i+1] = '\0'; memset(p[5]->name,0,N); strcpy(p[5]->name , "\nGATEWAY="); p[5]->name[9] = '\0'; memset(p[5]->value,0,N); for( i = 0;brdaddr_addr[i] != '\0';i++) p[5]->value[i] = brdaddr_addr[i]; p[5]->value[i] = '\0'; memset(p[6]->name,0,N); strcpy(p[6]->name , "\nHWADDR="); p[6]->name[8] = '\0'; memset(p[6]->value,0,N); for( i = 0;mac_addr[i] != '\0';i++) p[6]->value[i] = mac_addr[i]; p[6]->value[i+1] = '\0'; p[0]->next = p[1]; p[1]->next = p[2];p[2]->next = p[3];p[3]->next = p[4];p[4]->next = p[5]; p[5]->next = p[6];p[6]->next = p[7];p[7]->next = p[8];p[8]->next = NULL; //#ifdef DEBUG print(head); //#endif save(filename,head); /*打印修改后的配置文件*/ print(head); return 0; }
从一个文件中读取数据,进行操作后,再写到另一个文件中:
#include <stdio.h> #include <stdlib.h> #include <malloc.h> #include <string.h> #define LEN sizeof (struct gzb) #define MAXLEN 20 struct gzb { char name[MAXLEN]; float gz,sanj,gs,sd; struct gzb *next; }; float income_tax(float income,float insurance); void main() { FILE *fa,*fb; char buf[1024]; struct gzb *p1; /** ** a.txt ** ** 姓名 工资 三金 ** **/ if((fb=fopen("b.txt","w+"))==NULL) { printf("cannot open this file\n"); exit(1); } fprintf(fb,"姓名 工资 三金 税金 所得工资\n"); if((fa=fopen("a.txt","r"))==NULL) { printf("cannot open this a file\n"); exit(1); } while(fgets(buf,1024,fa)!=NULL)//此处可以做些技术性出来,是程序根据灵活性 { p1=(struct gzb*)malloc(sizeof(struct gzb)); sscanf(buf,"%s%f%f",p1->name,&p1->gz,&p1->sanj); printf("%s %f %f\n",p1->name,p1->gz,p1->sanj); p1->gs=income_tax(p1->gz,p1->sanj); fprintf(fb,"%s %f %f %f %f\n",p1->name,p1->gz,p1->sanj,p1->gs,p1->gz - p1->sanj - income_tax(p1->gz,p1->sanj)); } fclose(fa); fclose(fb); } float income_tax(float income,float insurance) { float taxincome,tax,rincome; taxincome=income-insurance-3500; if(taxincome<=0) tax=0; if(taxincome<=500) tax=taxincome*0.05; if(taxincome>500 && taxincome<=2000) tax=taxincome*0.1-25; if(taxincome>2000 && taxincome<=5000) tax=taxincome*0.15-125; if(taxincome>5000 && taxincome<=20000) tax=taxincome*0.2-375; if(taxincome>20000 && taxincome<=40000) tax=taxincome*0.25-1375; if(taxincome>40000 && taxincome<=60000) tax=taxincome*0.3-3375; if(taxincome>60000 && taxincome<=80000) tax=taxincome*0.35-6375; if(taxincome>80000 && taxincome<=100000) tax=taxincome*0.4-10375; if(taxincome>100000) tax=taxincome*0.45-1575; if(tax <= 0) tax = 0; return(tax); }