博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LinuxC语言读取文件,分割字符串,存入链表,放入另一个文件
阅读量:5959 次
发布时间:2019-06-19

本文共 4931 字,大约阅读时间需要 16 分钟。

//file_op.c

#include 
#include
#include
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
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 

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

 

转载地址:http://xgkax.baihongyu.com/

你可能感兴趣的文章
CentOS6.5+mysql5.1源码安装过程
查看>>
Js 笔记
查看>>
C++: find()函数的注意事项
查看>>
js的事件学习笔记
查看>>
leetcode 【 Add Two Numbers 】 python 实现
查看>>
Android接收系统广播
查看>>
将网络中的图片存为NSData并保存到sqlite的BLOB字段中
查看>>
Cocos2d-js-v3.2 在 mac 上配置环境以及编译到 Andorid 的注意事项(转)
查看>>
iOS用三种途径实现一方法有多个返回值
查看>>
python--class test
查看>>
从零开始理解JAVA事件处理机制(3)
查看>>
HttpURLConnection类的使用
查看>>
linux命令分析---SED (二)
查看>>
[INS-32025] 所选安装与指定 Oracle 主目录中已安装的软件冲突。
查看>>
py2与py3差别
查看>>
windows知识点
查看>>
第五章多态课后java_Java程序设计课后练习答案
查看>>
idea无用插件_没用过这些IDEA插件?怪不得写代码头疼
查看>>
linuxliveu盘怎么用_怎么用U盘重装系统?
查看>>
国际学院c语言作业,C语言程序的国际化
查看>>