//file_op.c
#include <string.h> #include <stdio.h> #include <stdlib.h> struct info{ int id; char name[10]; char sex[10]; char col[10]; char sub[15]; char marks[20]; struct info * prev; struct info * next; }; typedef struct info *st; static st head = NULL;//链表头指针 #define PRINT_ST(str) \ "info:id=%d; name=%s; sex=%s;col=%s; sub=%s; marks=%s\n",\ str->id,str->name, str->sex,str->col, str->sub, str->marks int temp = 1; int break_up(char *buffer);//分割字符串函数 int put_in(char* str[]);//放入结构体 int print_st(st str);//输出结构体,测试用 char * char_filter( char *str);//去掉行末回车符 int insert_list(st p);//插入链表 int main(void) { FILE *stream; char msg[100]; char backup[100]; st p1, p2; /* open a file for update */ stream = fopen("file.txt","r"); /* seek to the start of the file */ fseek(stream, 0, SEEK_SET);//指针指向文件头部 /*备份第一行内容*/ fgets(msg, 100, stream) != NULL; strcpy(backup, msg); /* 从第二行开始去数据 */ while( fgets(msg, 100, stream) != NULL) { printf("%s",msg); break_up(msg); memset(msg, 0, sizeof(msg)); } /*先读取一行内容,测试用 fgets(msg, 100, stream) != NULL; printf("%s",msg); break_up(msg); */ fclose(stream); /*正序输出链表,测试用*/ p1 = head; puts("\n"); while( p1 != NULL) { print_st(p1); p1 = p1->next; } /*倒序输出链表,测试用*/ p1 = head; puts("\n"); while(p1 != NULL) { p2 = p1; p1 = p1->next; } while(p2 != NULL) { print_st(p2); p2 = p2->prev; } /*下面新建文件,倒叙输出到一个新文件new.txt里面*/ stream = fopen("new.txt","w+"); if(fputs(backup, stream) < 0) { perror("fputs error:"); } p1 = head; while(p1 != NULL) { p2 = p1; p1 = p1->next; } while(p2 != NULL) { snprintf(msg, sizeof(msg), PRINT_ST(p2)); printf("test_char:%s\n",msg); fputs(msg, stream); p2 = p2->prev; } fclose(stream); /*释放链表*/ p1 = head->next; while (p1 != NULL) { p2 = p1; p1 = p1->next; free(p2); } free(head); head = NULL; return 0; } /*分割字符串*/ int break_up(char *buffer) { int i = 0, j = 0; char *p[20]= {NULL}; char *buf=buffer; char *outer_ptr=NULL; char *inner_ptr=NULL; while((p[i]=strtok_r(buf,";",&outer_ptr))!=NULL) { i++; buf=NULL; } // printf("Here we have %d strings\n",i);//测试用 for(j=0 ; j<i; j++) { printf("%s\n",p[j]);//输出分割字符串,测试用 } put_in(p); return 0; } /*放入结构体*/ int put_in(char* str[]) { st st1 = (st)malloc(sizeof(struct info)); st1->id = atoi(str[0]); strcpy(st1->name, str[1]); strcpy(st1->sex, str[2]); strcpy(st1->col, str[3]); strcpy(st1->sub, str[4]); str[5] = char_filter(str[5]); strcpy(st1->marks, str[5] ); st1->next = NULL; st1->prev = NULL; print_st(st1); if(temp == 1) { head = st1; temp++; return 0; } insert_list(st1); return 0; } int print_st(st str)// { /* printf("info:id=%d; name=%s; sex=%s; col=%s; sub=%s; marks=%s\n", str->id,str->name, str->sex, str->col, str->sub, str->marks); */ printf(PRINT_ST(str)); } char *char_filter( char *str) { int i = strlen(str); *(str + i - 1) = ‘\0‘; return str; } int insert_list(st p) { st q = head; while( q->next != NULL) { q = q->next; } q->next = p; p->prev = q; return 0; }
===================================
file.txt 内容
ID;NAME;SEX;COLLEGE;SUBJECT;REMARKS
1;jean;male;electron;communicate;no
marks
2;luce;female;legal;legal;thanks
3;devide;male;building;build;remarks
4;liulian;female;business;business;arm
===============================================
程序输出:
1;jean;male;electron;communicate;no
marks
1
jean
male
electron
communicate
no
marks
info:id=1; name=jean; sex=male;col=electron;
sub=communicate; marks=no
marks
2;luce;female;legal;legal;thanks
2
luce
female
legal
legal
thanks
info:id=2; name=luce; sex=female;col=legal;
sub=legal;
marks=thanks
3;devide;male;building;build;remarks
3
devide
male
building
build
remarks
info:id=3; name=devide;
sex=male;col=building; sub=build;
marks=remarks
4;liulian;female;business;business;arm
4
liulian
female
business
business
arm
info:id=4; name=liulian; sex=female;col=business; sub=business; marks=arm
info:id=1; name=jean; sex=male;col=electron;
sub=communicate; marks=no marks
info:id=2; name=luce;
sex=female;col=legal; sub=legal; marks=thanks
info:id=3; name=devide;
sex=male;col=building; sub=build; marks=remarks
info:id=4;
name=liulian; sex=female;col=business; sub=business; marks=arm
info:id=4; name=liulian;
sex=female;col=business; sub=business; marks=arm
info:id=3;
name=devide; sex=male;col=building; sub=build; marks=remarks
info:id=2;
name=luce; sex=female;col=legal; sub=legal; marks=thanks
info:id=1;
name=jean; sex=male;col=electron; sub=communicate; marks=no
marks
test_char:info:id=4; name=liulian; sex=female;col=business;
sub=business; marks=arm
test_char:info:id=3; name=devide; sex=male;col=building; sub=build; marks=remarks
test_char:info:id=2; name=luce; sex=female;col=legal; sub=legal; marks=thanks
test_char:info:id=1; name=jean; sex=male;col=electron; sub=communicate; marks=no marks