LinuxC语言读取文件,分割字符串,存入链表,放入另一个文件

//file_op.c

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
struct info{
int id;
char name[];
char sex[];
char col[];
char sub[];
char marks[];
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 = ;
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[];
char backup[];
st p1, p2;
/* open a file for update */
stream = fopen("file.txt","r");
/* seek to the start of the file */
fseek(stream, , SEEK_SET);//指针指向文件头部
/*备份第一行内容*/
fgets(msg, , stream) != NULL;
strcpy(backup, msg);
/* 从第二行开始去数据 */
while( fgets(msg, , stream) != NULL)
{
printf("%s",msg);
break_up(msg);
memset(msg, , 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) < )
{
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 ;
}
/*分割字符串*/
int break_up(char *buffer)
{
int i = , j = ;
char *p[]= {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= ; j<i; j++)
{
printf("%s\n",p[j]);//输出分割字符串,测试用
}
put_in(p);
return ;
}
/*放入结构体*/
int put_in(char* str[])
{
st st1 = (st)malloc(sizeof(struct info));
st1->id = atoi(str[]);
strcpy(st1->name, str[]);
strcpy(st1->sex, str[]);
strcpy(st1->col, str[]);
strcpy(st1->sub, str[]);
str[] = char_filter(str[]);
strcpy(st1->marks, str[] );
st1->next = NULL;
st1->prev = NULL;
print_st(st1);
if(temp == )
{
head = st1;
temp++;
return ;
}
insert_list(st1);
return ;
}
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 - ) = '\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 ;
}

===================================

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 

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 

luce 
female 
legal 
legal 
thanks

info:id=2; name=luce; sex=female;col=legal; sub=legal; marks=thanks 
3;devide;male;building;build;remarks 

devide 
male 
building 
build 
remarks

info:id=3; name=devide; sex=male;col=building; sub=build; marks=remarks 
4;liulian;female;business;business;arm 

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

上一篇:Android(java)学习笔记198:Android下的逐帧动画(Drawable Animation)


下一篇:MapKit框架使用