C++程序设计之标准库函数的使用

2025-10-26 15:33:38

1、C++语言提供了极为丰富的库函数。

C++程序设计之标准库函数的使用

2、C语言标准库中的各个函数,类型以及宏分别在以下标准头文件中说明:

assert.h 

float.h

math.h

stdarg.h

stdlib.h

ctype.h

limits.h

setjmp.h 

stddef.h 

string.h

errno.h 

locale.h  

signal.h  

stdio.h  

time.h

头文件声明格式为:

#include <头文件名>

3、文件操作

#include <stdio.h >

打开一个流可以将该流与一个文件或设备关联起来,这一关联可以通过关闭流而终止。

打开一个文件将返回一个指向FILE类型对象的指针,包含控制该流的所有必要的信息。

C++程序设计之标准库函数的使用

4、打开文件

    FILE * fopen(const char * filename, const char* mode);

关闭文件

    int fclose(FILE * stream);

删除文件

    int remove(const char* filename);

重命名文件

    int rename(const char * oldname, const char *newname);

C++程序设计之标准库函数的使用

5、格式化输入函数 nfscanf函数

int fscanf(FILE * stream, constchar * format, ...);

int scanf(const char* format, ...);

6、字符输入输出函数

fgetc函数 

    int fgetc(FILE * stream);

    以unsignedchar类型返回stream流中的一个字符。

fgets函数

    char * fgets(char *s, int n, FILE *stream);

    fgets函数用于读入最多n-1个字符到数组s中。

fputs函数

    int fputs(const char *s, FILE*stream);

    以unsignedchar类型返回stream流中的一个字符。

其他输入输出函数

int getc(FILE *stream);

int getchar(void);

char *gets(char *s);

int putc(int c, FILE * stream);

int putchar(int c);

int puts(const char *s);

int ungetc(int c , FILE * stream);

7、直接输入输出函数

fread函数

    size_t fread(void *ptr, size_t size size_t nobj, FILE *stream);

    读入最多nobj个长度为size的对象到ptr指向的数组中。

fwrite函数

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

    把ptr所指向的数组中nobj个长度为size的对象输出到流stream中。

8、文件定位函数

fseek函数

    int fseek(FILE *stream, longoffset, int origin);

fseek函数用于文件定位,读写操作将从新位置开始。

offset是偏移量

origin是平移参考位置:

   SEEK_SET(文件开始处) 

    SEEK_CUR(当前位置) 

    SEEK_END(文件结束处)

9、文件定位函数

ftell函数  

    long ftell(FILE *stream);

    返回与stream流相关的文件的当前位置。

rewind函数  

    void rewind(FILE*stream);

    等价于fseek(fp, 0L, SEEK_SET)与clearerr(fp)这两个函数顺序执行的效果。

10、错误处理函数

    当发生错误或到达文件末尾时,标准库中的许多函数将设置状态指示符。这些状态指示符可被显式地设置和测试。

void clearerr(FILE *stream);

int feof(FILE *stream);

int ferror(FILE *stream);

void perror(const char *s);

11、字符类测试

isalnum(c);  //函数isalpha(c)或 isdigit(c)为真。

isalpha(c);  //函数isupper(c)或 islower(c)为真。

iscntrl(c);  //c为控制字符。

isdigit(c);  //c十进制数字。

isgraph(c);  //c是除空格外的可打印字符。

islower(c);  //c是小写字母。

isprint(c);  //c是包括空格的可打印字符。

ispunct(c);  //c是除空格,字母和数字外的可打印字符。

isspace(c); // c是空格,换页符,换行符,回车符,横向制表符和纵向制表符。

isupper(c);  //c是大写字母。

isxdigit(c);  //c是十六进制数字。

int tolower(int c); //把c转换为小写字母

int toupper(int c);  //把c转换为大写字母

12、字符串函数

//返回一个指向字符串string中字符c最后一次出现的位置的指针,如果没有就返回NULL

char*strchr(const char *string,intc);

//返回字符串string中由字符串strCharSet中的字符构成的第一个子串的长度。

size_t strspn(constchar *string,   const char *strCharSet);

//返回字符串string中由不在字符串strCharSet中的字符组成的第一个字串的长度。

size_t strcspn(constchar *string,const char *strCharSet);

//返回指向字符串strCharSet中的任意字符第一次出现在字符串string中的位置的指针

char*strpbrk(const char *string,constchar *strCharSet);

//字符串查找

char*strstr(const char *string,constchar *strSearch );

size_t strlen(constchar *string );  //求字符串长度

char*strerror(int errnum); 

//找出strDelimit分割的下一个单词

char*strtok( char *strToken,const char *strDelimit );

13、内存函数

void *memcpy(void* dest,const void* src,size_t count );

void *memmove(void* dest, const void* src, size_t count );

int memcmp(const void* buf1,const void* buf2, size_t count );

void *memset(void* dest, int c, size_t count );

14、数学函数

C++程序设计之标准库函数的使用

15、断言函数

voidassert(int表达式);

其他函数

void abort(void);

void exit(int status);

int atexit(void  (*fcn)(void));

int system(const char*s);

char *getenv(const char *name)

void bsearch (const void *key,const void *base, size_t n,size_t size,

    int (*cmp)(const void *keyval, const void*datum) );

void qsort(void *base,size_t n, size_t size,int (*cmp)(const void *,const void *));

int abs(int n)

long labs(long n)

div_t div(int num, int denom)

ldiv_t ldiv(long num, long denom)

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢