`
jubincn
  • 浏览: 232444 次
  • 性别: Icon_minigender_1
  • 来自: 宁波
文章分类
社区版块
存档分类
最新评论

6.087 Practical Programming in C, lec10

 
阅读更多

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就是copymove就是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.

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

<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.

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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics