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
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
LinuxC语言读取文件,分割字符串,存入链表,放入另一个文件的更多相关文章
- Java基础知识强化之IO流笔记52:IO流练习之 把一个文件中的字符串排序后再写入另一个文件案例
1. 把一个文件中的字符串排序后再写入另一个文件 已知s.txt文件中有这样的一个字符串:"hcexfgijkamdnoqrzstuvwybpl" 请编写程序读取数据内容,把数据排 ...
- Oracle把逗号分割的字符串转换为可放入in的条件语句的字符数列
Oracle把逗号分割的字符串转换为可放入in的条件语句的字符数列 前台传来的字符串:'589,321' SELECT*FROM TAB_A T1 WHERE T1.CODE IN ( SEL ...
- Path,Files巩固,题目:从键盘接收两个文件夹路径,把其中一个文件夹中(包含内容)拷贝到另一个文件夹中
这个题目用传统的File,InputStream可以做,但是如果用Files,Path类做,虽然思路上会困难一些,但是代码简洁了很多,以下是代码: import java.io.IOException ...
- Java文件选择对话框(文件选择器JFileChooser)的使用:以一个文件加密器为例
文件加密器,操作过程肯定涉及到文件选择器的使用,所以这里以文件加密器为例.下例为我自己写的一个文件加密器,没什么特别的加密算法,只为演示文件选择器JFileChooser的使用. 加密器界面如图: 项 ...
- allegro把formate symbol文件从一个文件拷入另一个文件的方法
allegro画好PCB后经常需要添加一些说明谢谢,比如叠层信息.阻抗表等,但是每次都自己画太麻烦,现在就写下如何重复使用各种格式. 1.打开包含这些信息的板子,FILE-> Export -& ...
- git如何删除已经 add 的文件 (如何撤销已放入缓存区文件的修改)
使用 git rm 命令即可,有两种选择, 一种是 git rm –cached “文件路径”,不删除物理文件,仅将该文件从缓存中删除: 一种是 git rm –f “文件路径”,不仅将该文件从缓存中 ...
- Python文件读写 - 读一个文件所有行,加工后写另一个文件
#Filename: file_read_and_write.py #打开文件,cNames读取所有行,储存在列表中,循环对每一行在起始处加上序号1,2,3,4 with open(r'file/co ...
- c++鼠标点点,获取坐标值,放入到txt文件中
// oj3.cpp : Defines the entry point for the console application.// #include "stdafx.h"#in ...
- python 合并两个文件并将合并内容保存在另一个文件中
简单地文件合并方法 思路如下: 分别读取两个文件中的内容,并将其保存在一个列表中,将列表通过join()函数转为字符,并将新字符保存在新的文件中. 其中,test1.txt中的内容为: test2.t ...
随机推荐
- TypeError: 'module' object is not callable cp fromhttp://blog.csdn.net/huang9012/article/details/17417133
程序代码 class Person: #constructor def __init__(self,name,sex): self.Name = name ...
- 深入浅出Redis04使用Redis数据库(lists类型)
一 lists类型及操作 List是一个链表结构,主要功能是push,pop,获取一个范围的所有值等等,操作中key理解为链表的名字. Redis的list类型其实就是一个每个子元素都是sring类 ...
- javascript判断文件大小
<input type="file" id="fileName" name ="fileName" onchange="Ge ...
- char*和char []
1.char *s1 = "ssss"; 2.char s2[] = "bbbb"; 对于第一种,我是无法理解,无法想象字符串赋值给一个char类型的指针,查了 ...
- mysql大表如何优化
作者:哈哈链接:http://www.zhihu.com/question/19719997/answer/81930332来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处 ...
- linux+apache url大小写敏感问题
Linux对文件目录大小写敏感,URL大小写敏感会导致网页打不开,解决方法之一是启用Apache的mod_speling.so模块. 1.确认/usr/lib/httpd/modules目录下是否存在 ...
- httpModules与httpHandlers之httpModules(转载)
WapModule.cs:public class WapModule:IHttpModule{ public void Init(HttpApplication context) { ...
- WPF 组合快捷键
if(e.KeyStates == Keyboard.GetKeyStates(Key.F4) && Keyboard.Modifiers == ModifierKeys.Alt) { ...
- UrlConnection连接和Socket连接的区别
关于UrlConnection连接和Socket连接的区别,只知道其中的原理如下: 抽象一点的说,Socket只是一个供上层调用的抽象接口,隐躲了传输层协议的细节. urlconnection 基于H ...
- derby支持的数据类型
Data types This section describes the data types used in Derby. Built-In type overview Numeric types ...