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#等运行在虚拟机中的解释性语言不同,是彻底的编译运行,因此这种动态特性是由预处理的宏来实现的。
分享到:
相关推荐
lec10.ppt lec11.ppt lec12.ppt lec13.ppt lec14.ppt lec15.ppt lec16.ppt lec17.ppt lec18.ppt lec19.ppt lec2.ppt lec20.ppt lec21.ppt lec22.ppt lec23.ppt lec24.ppt lec25.ppt lec3.ppt lec4.ppt lec5.ppt lec6...
In the previous lecture, we leant about impedance spectroscopy. Electrochemical impedance spectroscopy is the technique where the cell or electrode impedance is platted versus frequency. Thus, the ...
programming in computing 10a lec2
EI374 高级算法-全套 PPT 课件-笔记 lec1-slides.pdf lec1.pdf lec2-slides.pdf lec2.pdf lec3-slides.pdf lec3.pdf lec4-slides.pdf ...lec6.pdf lec7.pdf lec8.pdf lec9.pdf lec10.pdf lec11.pdf
Lec10_review.pptx
Lec 10 Collision Detection and Response Quiz Lec 11 Ray Casting and Rendering Lec 12 Ray Casting II Lec 13 Ray Tracing Lec 14 Acceleration Structures for Ray Casting Assignment 3 Lec 15 Shading ...
数字逻辑设计及应用教学英文课件:Lec10-Chap 6.ppt
算法设计与分析:6-Lec10.pdf
EI338 计算机系统工程-Computer Systems Engineering-全套 PPT 课件 CA-lec1.pdf ...lec6-OS.pdf lec7-OS.pdf lec8-OS.pdf lec9-OS.pdf lec10-OS.pdf lec11-OS.pdf lec12-OS.pdf Study-Guide.pdf Summary.pdf
麻省理工matlab课件-MIT6_094IAP10_lec04.pdf 本帖最后由 sunchy11 于 2012-2-8 15:46 编辑 分享个MIT的matlab 教程,属于初级入门,希望对大家有帮助哈。
lec4.pptx
算法设计与分析:6-Lec10-Applications.pdf
算法设计与分析:6-Lec10-Matching.pdf
可通过改程序进行危险源的LEC分值的客观化处理。
算法设计与分析:2-Lec6 .pdf
算法设计与分析:2-Lec6.pdf
6 PNNI: Private Network Node Interface ...................... 151 6.1 Introduction ................................... 151 6.1.1 Introduction to the PNNI Routing Protocol .............. 151 6.1.2...
麻省理工matlab课件-MIT6_094IAP10_lec05.pdf 本帖最后由 sunchy11 于 2012-2-8 15:46 编辑 分享个MIT的matlab 教程,属于初级入门,希望对大家有帮助哈。