strtok的用法(文件操作)
strtok :在一个字符串查找下一个符号
char *strtok( char *strToken, const char *strDelimit );
返回值:返回指向在strToken字符串找到的下一个符号的指针,当在字符串找不到符号时,将返回NULL.每
次调用都通过用NULL字符替代在strToken字符串遇到的分隔符来修改strToken字符串.
如果strtok函数第一个参数不是NULL,函数将找到字符串的第一个标记。strtok同时保存它在字符串中的位置。如果第一个参数是NULL,函数就在同一个字符串中从这个被保存的位置开始像前面一样查找下一个标记。字符串不存在更多的标记,则返回NULL;
];
;
#if 0
array[index] = strtok(buf, " ");
while(array[index] != NULL){
printf("%s\n", array[index]);
index ++;
array[index] = strtok(NULL, " ");
}
#else
for(array[index] = strtok(buf, " "); array[index] != NULL; index++,\
array[index] = strtok(NULL, " "))
printf("%s\n", array[index]);
#endif
以下是实际应用中的一段代码:
/*read file*/
msg_t *read_t()
{
;
;
];
char* result = NULL;
],tmp[];
FILE *fp;
char delims[] = " ";
memset(a,,);
// msg_t * msg = NULL;
if((fp = fopen("./c.txt","r"))== NULL)
{
perror("fopen");
}
,fp) != NULL)//读取文件
{
printf("%s",buf);
a[i] = strtok(buf,delims);
while(a[i] != NULL) {//进行切割
printf("%s\n",a[i]);
i++;
a[i] = strtok(NULL,delims);
}
#if DEBUG
msg[j].type = *(a[]);
strcpy(msg[j].name,a[]);
strcpy(msg[j].pass,a[]);
msg[j].age = atoi(a[]);
strcpy(msg[j].sex,a[]);
msg[j].salary = atoi(a[]);
memset(a,,);
j++;
i = ;
msg[j].type = '#';
#endif
}
fclose(fp);
return msg;
}
/*write file*/
int write_t(msg_t *msg) {
;
;
];
char* result = NULL;
],tmp[];
FILE *fp;
char delims[] = " ";
memset(a,,);
#if DEBUG
while(msg->type != '#'){
printf("%c\t",msg->type);
printf("%s\t",msg->name);
printf("%s\t",msg->pass);
printf("%5d\t",msg->age);
printf("%s\t",msg->sex);
printf("%5d\n",msg->salary);
msg++;
}
#endif
if((fp = fopen("./a.txt","a")) == NULL){
;
}
memset(buf,,);
sprintf(buf,"%c %s %s %d %s %d\n",msg->type,msg->name,msg->pass,
msg->age,msg->sex,msg->salary);//字符串拼写
fwrite(buf,,fp);//写入文件
fclose(fp);
;
}
strtok的用法(文件操作)的更多相关文章
- Python3中IO文件操作的常见用法
首先创建一个文件操作对象: f = open(file, mode, encoding) file指定文件的路径,可以是绝对路径,也可以是相对路径 文件的常见mode: mode = “r” # ...
- [python]打开文件操作open用法
1. 格式 handle = open(file_name, access_mode = 'r') file_name: 希望打开的文件名 access_mode: 'r'表示读取,'w'表示写入,' ...
- 强大的pdf文件操作小工具——PDFtk的小白用法 【转载】
转载出处https://www.cnblogs.com/basterdaidai/p/6204518.html 前言 作为程序员,大家都知道的,总是会被技术小白问各种跟编程没什么关系的硬件.软件问题. ...
- Python之路第一课Day3--随堂笔记(文件操作)
一.集合的介绍 1.集合操作 集合是一个无序的,不重复的数据组合,它的主要作用如下: 去重,把一个列表变成集合,就自动去重了 关系测试,测试两组数据之前的交集.差集.并集等关系 常用操作 s = se ...
- 003-Tuple、Array、Map与文件操作入门实战
003-Tuple.Array.Map与文件操作入门实战 Tuple 各个元素可以类型不同 注意索引的方式 下标从1开始 灵活 Array 注意for循环的until用法 数组的索引方式 上面的for ...
- C的文件操作2
[转] C语言文件操作 概述 所谓文件(file)一般指存储在外部介质上数据的集合,比如我们经常使用的mp3.mp4.txt.bmp.jpg.exe.rmvb等等.这些文件各有各的用途,我们通常将它 ...
- (Python )格式化输出、文件操作、json
本节学习Python的格式化输出,文件操作以及json的简单用法 1.格式化输出 将非字符串类型转换成字符串,可以使用函数:str() 或者repr() ,(这两个函数的区别目前我还没搞懂,求解答) ...
- Python :open文件操作,配合read()使用!
python:open/文件操作 open/文件操作f=open('/tmp/hello','w') #open(路径+文件名,读写模式) 如何打开文件 handle=open(file_name,a ...
- python学习笔记-(七)python基础--集合、文件操作&函数
本节内容 1.集合操作 2.文件操作 3.字符编码与转码 4.函数操作 1.集合操作 集合是一个无序的.不重复的数据组合: 1.1 常用操作 它的作用是: 1)自动去重:列表变成集合,自动去重: &g ...
随机推荐
- BZOJ3437 小P的牧场 【斜率优化dp】
3437: 小P的牧场 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 1502 Solved: 836 [Submit][Status][Disc ...
- bzoj3232
Description DZY家的后院有一块地,由N行M列的方格组成,格子内种的菜有一定的价值,并且每一条单位长度的格线有一定的费用. DZY喜欢在地里散步.他总是从任意一个格点出发,沿着格线行走直到 ...
- Linux下 ps -ef 和 ps aux 的区别及格式详解
原文:https://www.cnblogs.com/5201351/p/4206461.html Linux下ps -ef和ps aux的区别及格式详解 Linux下显示系统进程的命令ps,最常用的 ...
- Educational Codeforces Round 6 B
B. Grandfather Dovlet’s calculator time limit per test 1 second memory limit per test 256 megabytes ...
- 题解【luogu P2421 bzoj P1407 [NOI2002]荒岛野人】
洛谷题目链接 bzoj题目链接 题目大意:给定\(n\)组\(C_i, P_i, L_i\),求最小的\(M\)使得对于任意的\(i,j (1 \leq i, j \leq n)\) \[C_i + ...
- hdu 4408 Minimum Spanning Tree
Problem Description XXX is very interested in algorithm. After learning the Prim algorithm and Krusk ...
- NOIP2006 数列
codevs 1141 数列 http://codevs.cn/problem/1141/ 2006年NOIP全国联赛普及组 时间限制: 1 s 空间限制: 128000 KB 题目描述 ...
- poj 3376 Finding Palindromes
Finding Palindromes http://poj.org/problem?id=3376 Time Limit: 10000MS Memory Limit: 262144K ...
- Ubuntu12.04 SVN安装过程
一.安装SVN和配置SVN 1.安装SVN apt-get install subversion 2.创建SVN目录,项目目录和配置文件目录 mkdir /var/svn mkdir /var/svn ...
- 持续集成之Jenkins安装部署
1.系统环境和安装java环境 [root@devops ~]# cat /etc/redhat-release CentOS Linux release 7.3.1611 (Core) 安装java ...