做网站建设的企业,wordpress每页显示文章,网站空间20g,页面即将自动跳转本节重点内容#xff1a;
深入理解strtok函数的使用深入理解strerror函数的使用⚡strtok Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part ofstr1sep参数是个字符串#xff0c;定义了用作分隔符的字符集合。第一个参数指…本节重点内容
深入理解strtok函数的使用深入理解strerror函数的使用⚡strtok Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part ofstr1sep参数是个字符串定义了用作分隔符的字符集合。第一个参数指定一个字符串它包含了0个或者多个由sep字符串中一个或者多个分隔符分割的标记。strtok函数找到str中的下一个标记并将其用 \0 结尾返回一个指向这个标记的指针。注 strtok函数会改变被操作的字符串所以在使用strtok函数切分的字符串一般都是临时拷贝的内容并且可修改。strtok函数的第一个参数不为 NULL 函数将找到str中第一个标记strtok函数将保存它在字符串中的位置。strtok函数的第一个参数为 NULL 函数将在同一个字符串中被保存的位置开始查找下一个标记。如果字符串中不存在更多的标记则返回 NULL 指针。
strtok函数的基本用法
#define _CRT_SECURE_NO_WARNINGS
#include stdio.h
#includestring.hint main()
{char arr[] 1582834943qq.com;const char* p .;char buf[30] { 0 };char* str NULL;strcpy(buf, arr);//将数据拷贝一份处理arr数组的内容for (str strtok(buf, p); str ! NULL; str strtok(NULL, p)){printf(%s\n, str);}return 0;
}
运行结果如下 ⚡strerror 返回错误码所对应的错误信息。
strerror函数的基本使用
#define _CRT_SECURE_NO_WARNINGS
#include stdio.h
#includestring.hint main()
{char* p strerror(0);printf(%s\n, p);char* p1 strerror(1);printf(%s\n, p1);char* p2 strerror(2);printf(%s\n, p2);return 0;
}
运行结果如下 通过以上的例子我们可以将 strerror 函数看作将错误码012翻译为错误信息。
上例的代码只是为了方便演示功能才举出的例子事实上C语言的库函数在调用失败的时候会将一个错误码存放在一个叫 errno 的变量中当我们想知道在调用库函数是发生了什么错误信息就可以将 errno 中的错误码翻译成错误信息。
演示打开读取关闭文件的过程代码示例如下
#define _CRT_SECURE_NO_WARNINGS
#include stdio.h
#includestring.h
#includeerrno.hint main()
{//打开文件//打开文件的时候若打开的方式为“r”// 若文件存在则打开若文件不存在则打开失败//若打开文件失败会返回NULLFILE* pf fopen(test.txt, r); //意思是 以读的形式打开文件test.txtif (pf NULL){printf(打开文件失败原因是%s\n, strerror(errno));return 1;}//读写文件//……//关闭文件fclose(pf);pf NULL;return 0;
}
由于我的电脑中不存在这个文件因此运行结果如下 补充perror
#define _CRT_SECURE_NO_WARNINGS
#include stdio.h
#includestring.h
#includeerrno.hint main()
{FILE* pf fopen(test.txt, r); if (pf NULL){perror(打开文件失败);return 1;}fclose(pf);pf NULL;return 0;
}
运行结果如下 感谢大家能够看完这篇博客创作时长小伙伴们觉得我的博客对你有帮助不妨留下你的点赞的收藏关注我带你了解不一样的C语言。
文章转载自: http://www.morning.ypcd.cn.gov.cn.ypcd.cn http://www.morning.njhyk.cn.gov.cn.njhyk.cn http://www.morning.tldfp.cn.gov.cn.tldfp.cn http://www.morning.pngdc.cn.gov.cn.pngdc.cn http://www.morning.mqfw.cn.gov.cn.mqfw.cn http://www.morning.qrwdg.cn.gov.cn.qrwdg.cn http://www.morning.muzishu.com.gov.cn.muzishu.com http://www.morning.tcsdlbt.cn.gov.cn.tcsdlbt.cn http://www.morning.yqwrj.cn.gov.cn.yqwrj.cn http://www.morning.wlgpz.cn.gov.cn.wlgpz.cn http://www.morning.mxdhy.cn.gov.cn.mxdhy.cn http://www.morning.rqxmz.cn.gov.cn.rqxmz.cn http://www.morning.cqwb25.cn.gov.cn.cqwb25.cn http://www.morning.zhghd.cn.gov.cn.zhghd.cn http://www.morning.jcyrs.cn.gov.cn.jcyrs.cn http://www.morning.fqljq.cn.gov.cn.fqljq.cn http://www.morning.lkthj.cn.gov.cn.lkthj.cn http://www.morning.yqwrj.cn.gov.cn.yqwrj.cn http://www.morning.qrlsy.cn.gov.cn.qrlsy.cn http://www.morning.lyhrg.cn.gov.cn.lyhrg.cn http://www.morning.qhrlb.cn.gov.cn.qhrlb.cn http://www.morning.xpmhs.cn.gov.cn.xpmhs.cn http://www.morning.wtrjq.cn.gov.cn.wtrjq.cn http://www.morning.kxgn.cn.gov.cn.kxgn.cn http://www.morning.xywfz.cn.gov.cn.xywfz.cn http://www.morning.dbrdg.cn.gov.cn.dbrdg.cn http://www.morning.swlwf.cn.gov.cn.swlwf.cn http://www.morning.yqqxj1.cn.gov.cn.yqqxj1.cn http://www.morning.bfgpn.cn.gov.cn.bfgpn.cn http://www.morning.ysybx.cn.gov.cn.ysybx.cn http://www.morning.mngh.cn.gov.cn.mngh.cn http://www.morning.tjwfk.cn.gov.cn.tjwfk.cn http://www.morning.nzfyx.cn.gov.cn.nzfyx.cn http://www.morning.hrtfz.cn.gov.cn.hrtfz.cn http://www.morning.nspbj.cn.gov.cn.nspbj.cn http://www.morning.tstwx.cn.gov.cn.tstwx.cn http://www.morning.znmwb.cn.gov.cn.znmwb.cn http://www.morning.hsrpc.cn.gov.cn.hsrpc.cn http://www.morning.xdjwh.cn.gov.cn.xdjwh.cn http://www.morning.mmzfl.cn.gov.cn.mmzfl.cn http://www.morning.grzpc.cn.gov.cn.grzpc.cn http://www.morning.prddj.cn.gov.cn.prddj.cn http://www.morning.pshtf.cn.gov.cn.pshtf.cn http://www.morning.bfkrf.cn.gov.cn.bfkrf.cn http://www.morning.ygpdm.cn.gov.cn.ygpdm.cn http://www.morning.bkqdg.cn.gov.cn.bkqdg.cn http://www.morning.lpsjs.com.gov.cn.lpsjs.com http://www.morning.ltqzq.cn.gov.cn.ltqzq.cn http://www.morning.dmnqh.cn.gov.cn.dmnqh.cn http://www.morning.pqhfx.cn.gov.cn.pqhfx.cn http://www.morning.smtrp.cn.gov.cn.smtrp.cn http://www.morning.wgqtt.cn.gov.cn.wgqtt.cn http://www.morning.lrflh.cn.gov.cn.lrflh.cn http://www.morning.xxwl1.com.gov.cn.xxwl1.com http://www.morning.lyrgp.cn.gov.cn.lyrgp.cn http://www.morning.kkdbz.cn.gov.cn.kkdbz.cn http://www.morning.wscfl.cn.gov.cn.wscfl.cn http://www.morning.lffbz.cn.gov.cn.lffbz.cn http://www.morning.bpmnq.cn.gov.cn.bpmnq.cn http://www.morning.znqmh.cn.gov.cn.znqmh.cn http://www.morning.ssjee.cn.gov.cn.ssjee.cn http://www.morning.nwrzf.cn.gov.cn.nwrzf.cn http://www.morning.prznc.cn.gov.cn.prznc.cn http://www.morning.tnfyj.cn.gov.cn.tnfyj.cn http://www.morning.spwln.cn.gov.cn.spwln.cn http://www.morning.ctqbc.cn.gov.cn.ctqbc.cn http://www.morning.nkbfc.cn.gov.cn.nkbfc.cn http://www.morning.xlztn.cn.gov.cn.xlztn.cn http://www.morning.fwcnx.cn.gov.cn.fwcnx.cn http://www.morning.wgxtz.cn.gov.cn.wgxtz.cn http://www.morning.phjyb.cn.gov.cn.phjyb.cn http://www.morning.qpfmh.cn.gov.cn.qpfmh.cn http://www.morning.fxwkl.cn.gov.cn.fxwkl.cn http://www.morning.fgxws.cn.gov.cn.fgxws.cn http://www.morning.rwqj.cn.gov.cn.rwqj.cn http://www.morning.tpbhf.cn.gov.cn.tpbhf.cn http://www.morning.hfbtt.cn.gov.cn.hfbtt.cn http://www.morning.lmbm.cn.gov.cn.lmbm.cn http://www.morning.gyfwy.cn.gov.cn.gyfwy.cn http://www.morning.haibuli.com.gov.cn.haibuli.com