C standard library: stdio.h,ctype.h, stdlib.h, assert.h, stdarg.h, time.h

<stdio.h>: File operations

int remove(const char∗ filename)

• removes the file from the filesystem.

• retrn non-zero on error.

int rename(const char∗ oldname,const char∗ newname)

• renames file

• returns non-zero on error(reasons?: permission, existence)

第一次发现C的<stdio.h>中有这两个函数,一直觉得文件操作很复杂和神秘,现在看还是很简单的。不过底层应该会涉及到安全机制,尤其是在涉及到多种操作系统时,不同的安全策略也许会带来很大的麻烦。

<stdio.h>:Temporary files

FILE∗ tmpfile(void)

• creates a temporary file withmode "wb+".

• the file is removed automaticallywhen program terminates.

char∗ tmpnam(char s[L_tmpnam])

• creates a string that is not thename of an existing file.

• return reference to internalstatic array if s is NULL. Populate s otherwise.

• generates a new name every call.

这个临时的命名用时间来命名是最合适的了。\

<stdio.h>: Raw I/O

size_t fread(void∗ ptr , size_t size, size_t nobj,FILE∗stream)

• reads at most nobj items of sizesize from stream into ptr.

• returns the number of items read.

• feof and ferror must be used totest end of file.

size_t fwrite (const void∗ ptr,size_t size, size_t nobj,FILE∗stream)

• write at most nobj items of sizesize from ptr onto stream.

• returns number of objects written.

fread使我想起了malloc时对所获得的内存进行强制类型转换,大概也是这个样子吧,将新的内存段按大小分,再登记下这段内存的类型。

<stdio.h>: File position

int fseek(FILE∗ stream, long offset,int origin )

• sets file position in the stream.Subsequent read/write begins at this location

• origin can be SEEK_SET, SEEK_CUR,SEEK_END.

• returns non-zero on error.

long ftell (FILE∗ stream)

• returns the current positionwithin the file. (limitation? long data type).

• returns -1L on error.

int rewind(FILE∗ stream)

• sets the file pointer at thebeginning.

• equivalent tofseek(stream,0L,SEEK_SET);

这几个函数用的多一些,记得上课时就学过。在进行文件流操作时,至少要记录文件的起始位置和当前文件指针的位置。

<stdio.h>: File errors

void clearerr (FILE∗ stream)

• clears EOF and other errorindicators on stream. int feof (FILE∗ stream)

int feof (FILE∗ stream)

• return non-zero (TRUE) if end offile indicator is set for stream.

• only way to test end of file forfunctions such as fwrite(),fread()

int ferror (FILE∗ stream)

• returns non-zero (TRUE) if anyerror indicator is set for stream.

<string.h>: Memory functions

void∗ memcpy(void∗ dst,const void∗ src,size_t n)

• copies n bytes from src tolocation dst

• returns a pointer to dst.

• src and dst cannot overlap.

void∗ memmove(void∗ dst,const void∗ src,size_t n)

• behaves same as memcpy() function.

• src and dst can overlap.

int memcmp(const void∗ cs,const void∗ ct,int n)

• compares first n bytes between csand ct.

void∗ memset(void∗ dst,int c,int n)

• fills the first n bytes of dstwith the value c.

• returns a pointer to dst

memcpy相对memmove最大的好处就是方便并行化处理吧。这两个名字起的很精彩,copy就是copy,move就是move。在设计类库时多考虑下数据的独立性是很好的编程习惯。

<stdlib.h>:Utility

double atof(const char∗ s)

int atoi (const char∗ s)

long atol(const char∗ s)

• converts character to float,integerand long respectively.

int rand()

• returns a pseduo-random numbersbetween 0 and RAND_MAX

void srand(unsigned int seed)

• sets the seed for thepseudo-random generator!

前面的几个函数实现起来好像并不难。就是整型转浮点的实现可能麻烦一些,我还想不到特别优雅的方法。

<stdlib.h>: Exiting

void abort(void)

• causes the program to terminateabnormally.

void exit ( int status)

• causes normal program termination.The value status is returned to the operating system.

• 0 EXIT_SUCCESS indicatessuccessful termination. Any other value indicates failure(EXIT_FAILURE)

void atexit (void (∗fcn )( void))

• registers a function fcn to becalled when the program terminates normally;

• returns non zero when registrationcannot be made.

• After exit() is called, thefunctions are called in reverse order of registration.

int system(const char∗ cmd)

• executes the command in stringcmd.

• if cmd is not null, the programexecutes the command and returns exit status returned by the command

system这个函数好像python里面也有,并且在python里面用的很多。atexit的执行顺序为什么是逆序,实在想不通。

<stdlib.h>:Searchign and sortin

void∗ bsearch(const void∗ key, const void∗ base, size_t n,size_t size, int(∗cmp)(const void∗ keyval, const void∗ datum));

• searches base[0] through base[n-1] for *key.

• function cmp() is used to perform comparison.

• returns a pointer to the matching item if it exists and NULLotherwise.

void qsort(void∗ base, size_t n, size_t sz, int(∗cmp)(constvoid∗, const void∗));

• sorts base[0] through base[n-1] in ascending/descending order.

• function cmp() is used to perform comparison.

搜索和排序中的比较函数指针是使用函数指针的一个典型例子。bsearch和qsearch如此常用,以至于很多编程语言的库函数都将其包含在内。

<assert.h>: Diagnostics

void assert(int expression)

• used to check for invariants/codeconsistency during debugging

• does nothing when expression istrue.

• prints an error messageindicating, expression, filename and line number.

Alternative ways to print filename and line number duringexecution is to use: __FILE__,__LINE__ macros.

assert函数在发布为release时应该会去掉,现在也明白了为什么JUnit里面那么多aassert打头的函数了。

<stdarg.h>:Variable argument lists

Variable argument lists:

• functions can variable number ofarguments.

• the data type of the argument canbe different for each argument.

• atleast one mandatory argument isrequired.

• Declaration:

int printf (char∗ fmt ,...); /∗fmt is last named argument∗/

va_list ap

• ap defines an iterator that willpoint to the variable argument.

• before using, it has to beinitialized using va_start.

C与Java、C#等运行在虚拟机中的解释性语言不同,是彻底的编译运行,因此这种动态特性是由预处理的宏来实现的。

6.087 Practical Programming in C, lec10相关推荐

  1. Programming: Principles and Practice Using C++

    这本书已经看完了几个月,一直想写点什么,又不知从何说起.今天看到Linus对C++的一些批评,和这本书结合起来看,还有点意思.(Linus对C++的批评不是偶然的心血来潮,07年的时候就说过" ...

  2. Entity Framework 4.3 中的新特性

    原文地址:http://www.cnblogs.com/supercpp/archive/2012/02/20/2354751.html EF4.3于2月9号正式发布了,微软的EF小组最近一年开始发力 ...

  3. python编程基础与应用-有哪些适合零编程基础的人学习Python的书?

    筛选了2年内优秀的python书籍,个别经典的书籍扩展到5年内. python现在的主流版本是3.7(有明显性能提升,强烈推荐) 3.6, 不基于这两个或者更新版本的书,慎重选择.很多库已经不提供py ...

  4. python经典好书-有哪些 Python 经典书籍?

    内容太长,完整内容请访问原文: python 3.7极速入门教程9最佳python中文工具书籍下载 筛选了2年内优秀的python书籍,个别经典的书籍扩展到5年内. python现在的主流版本是3.7 ...

  5. python推荐书籍-有哪些 Python 经典书籍?

    内容太长,完整内容请访问原文: python 3.7极速入门教程9最佳python中文工具书籍下载 筛选了2年内优秀的python书籍,个别经典的书籍扩展到5年内. python现在的主流版本是3.7 ...

  6. python资料下载-python电子书学习资料打包分享百度云资源下载

    [300dpi高清版] Python基砒教程(第2版)LHD,pdf Head. First. Python中文版pdf [ Python3程序开发指南第二版pdf [ thon编程第4版)]( Pr ...

  7. 推荐一些C++经典书籍

    c++程序设计教程  c++编程思想  c++大学教程  c++程序设计语言  数据结构算法与应用c++语言描述  c++标准模板库------自修教程与参考手册  泛型编程与STL  深度探索c++ ...

  8. Fibonacci(斐波纳契)数列各种优化解法

    Fibonacci数列: 描述了动物繁殖数量.植物花序变化等自然规律.作为一个经典的数学问题,Fibonacci数列常作为例子出现在程序设计.数据结构与算法等多个相关学科中. 下面简单地分析一下常见的 ...

  9. [转]NS2 Data Collections by mitkook

    原发表在偶的Opera Blog: http://my.opera.com/mitkook/blog/show.dml/354188 Collected by mitkook! NS2在Windows ...

最新文章

  1. 谈谈Python那些不为人知的冷知识(二)
  2. Object​.assign()
  3. 巧用find命令清除系统垃圾
  4. CLOUD 04:zookeeper,kafka,hadoop高可用
  5. OD使用教程 调试篇
  6. Oracle查询会话连接数
  7. Jenkins 设置镜像_Windows Docker Agent 镜像可以常规使用了
  8. IDEA 一直不停的scanning files to index解决办法
  9. bzoj 4278 [ONTAK2015]Tasowanie——后缀数组
  10. 在PyTorch中转换数据
  11. Shiro授权流程图
  12. 转换预定义的字符为html实体,php把一些预定义的 HTML 实体转换为字符。
  13. jpa jql 时间范围查询_SpringBoot整合JPA案例
  14. 如何用开源经历为你的简历增加光彩
  15. qt socket 传递结构体 结构体中有list_GO语言入门-14、结构体
  16. SpringBoot中发送QQ邮件
  17. 202012月计算机考试时间,年全国计算机等级考试时间(范文).docx
  18. 张国荣一生57部电影海报全集
  19. 创始人、CEO、总裁和董事长到底谁更大?
  20. ctf ddos数据包 杂项 流量_抗DDoS攻击设备化解危机于无形

热门文章

  1. 网站seo优化,网站SEO优化方案
  2. 西电2020计算机考研,西安电子科技大学研究生院,西电2020年考研成绩最新信息!...
  3. 百度周景博:POI知识图谱的构建及应用
  4. 期货都有哪些类型和玩法?
  5. 二十四节气之大暑时节常识介绍
  6. 1.9Hadoop插件
  7. python中字典的循环遍历的两种方式
  8. 微软Windows 11正式发布!(文末送书)
  9. oracle 移动分区表到指定表空间,及修改表的默认表空间
  10. 一个非常复杂的某考核系统计算考核得分代码层设计