美文网首页Linux/C
linux c 标准库函数

linux c 标准库函数

作者: 国服最坑开发 | 来源:发表于2019-12-25 11:40 被阅读0次
    • assert.h 断言, 不满足就退出程序
    assert()
    
    • ctype.h 入参是一个 char''
        printf("isalnum : %d\n", isalnum('a'));
        printf("isalpha : %d\n", isalpha('a'));
        printf("iscntrl : %d\n", iscntrl('\n'));
        printf("isdigit : %d\n", isdigit('1'));
        printf("isgraph : %d\n", isgraph('m'));
        printf("islower : %d\n", islower('A'));
        printf("isupper : %d\n", isupper('A'));
        printf("isprint : %d\n", isprint('\t'));
        printf("ispunct : %d\n", ispunct('\t'));
        printf("isxdigit: %d\n", isxdigit('f'));
    
    • errno.h 包含一个全局变量 errno
    #include <errno.h>
    #include <string.h>
    
    
        FILE *fp;
        fp = fopen("file.txt", "r");
        if (fp == NULL){
            printf("%d\n", errno);
            printf("%s\n", strerror(errno));
        }
        else {
            fclose(fp);
        }
    -----
    2
    No such file or directory
    
    • float.h :定义float相关常量
        printf("%f\n", FLT_MAX);
    ----
    340282346638528859811704183484516925440.000000
    
    • limits.h 定义char,int 之类的类型的最大值常量
        printf("%d\n", INT_MAX);
    ----
    2147483647
    
    • locale.h 区域相关处理
    setlocale() ,设置时区, 会影响时间参数输出
    localeconv(), 返回时区设置全部信息
    
    • math.h 数学库

    • setjmp.h 全局跨函数跳转

    #include <stdio.h>
    #include <setjmp.h>
     
    static jmp_buf buf;
     
    void second(void) {
        printf("second\n");         // 打印
        longjmp(buf,1);             // 跳回setjmp的调用处 - 使得setjmp返回值为1
    }
     
    void first(void) {
        second();
        printf("first\n");          // 不可能执行到此行
    }
     
    int main() {   
        if ( ! setjmp(buf) ) {
            first();                // 进入此行前,setjmp返回0
        } else {                    // 当longjmp跳转回,setjmp返回1,因此进入此行
            printf("main\n");       // 打印
        }
     
        return 0;
    }
    ----
    second
    main
    
    • signal.h
    signal() 信号监听处理程序
    raise() 发出一个信号
    
    #include <stdlib.h>
    #include <stdio.h>
    #include <signal.h>
    #include <unistd.h>
    
    void sighandler(int signum){
        printf("received signal :%d\n", signum);
        exit(1);
    }
    int main(){
        signal(SIGINT, sighandler);
    
        while(1){
            printf("sleeping ...\n");
            sleep(1);
        }
    }
    
    
    int main() {
        signal(SIGINT, sighandler);
        sleep(1);
        raise(SIGINT);
    }
    
    
    • stdarg.h 多参数
    va_list
    va_start
    va_end
    
    int sum(int num_args, ...){
        int val = 0;
        va_list ap;
        int i;
    
        va_start(ap, num_args);
        for (i = 0; i < num_args; ++i) {
            val += va_arg(ap, int);
        }
        va_end(ap);
    
        return val;
    }
    
        printf("%d\n", sum(3, 10, 20, 30));
    
    
    • stddef.h 宏定义
    NULL
    offsetof : 结构体中 成员变量的偏移大小
    
        struct address {
            char name[50];
            char street[50];
            int phone;
        };
        printf("offset is %d\n", offsetof(struct address, phone));
    
    ---- 
    100
    
    • stdio.h 标准输入输出
    size_t
    FILE
    EOF
    
    fopen()
    fclose()
    fgetc()
    ferror() 判断 FILE 是否有错 errno
    clearerr(fp) 清除errno
    fflush() 写文件缓冲区
    
    void testfflush(){
        char buff[1024];
        memset(buff, '\0', sizeof(buff));
    
        fprintf(stdout, "start full cache \n");
        setvbuf(stdout, buff, _IOFBF, 1024);
    
        fprintf(stdout, "hello ag\n");
        fprintf(stdout, "will save to buff \n");
        fflush(stdout);
    
        fprintf(stdout, "will show in \n");
        fprintf(stdout, "last 5 seconds \n");
    
        sleep(5);
    }
    
    
    fgetpos : 取文件游标位置
    fsetpos : 设置游标位置, 
    void testfgetpos(){
        FILE *fp;
        fpos_t postion;
    
        fp = fopen("file.txt", "w+");
        fgetpos(fp, &postion);
    
        fputs("Hello, world!", fp);
    
        fsetpos(fp, &postion);
        fputs("over write before", fp);
        fclose(fp);
    }
    
    fread , 读取到内存
    void testfread() {
        FILE *fp;
        char c[] = "This is runoob";
        char buffer[20];
    
        fp = fopen("file.txt", "w+");
    
        fwrite(c, strlen(c) + 1, 1, fp);
        fseek(fp, 0, SEEK_SET);
    
        fread(buffer, strlen(c) + 1, 1, fp);
    
        printf("%s\n", buffer);
        fclose(fp);
    }
    
    freopen () : 关闭旧流, 输出到新流
    void testreopen(){
        FILE *fp;
        printf("will be show in stdout\n");
        // 关闭掉stdout, 把后续的 printf 输出到文件里
        fp = freopen("file.txt", "w+", stdout);
        printf("will be show in file.txt\n");
        fclose(fp);
    }
    
    fseek() : 调整指针位置
      SEEK_SET  文件的开头
      SEEK_CUR  文件指针的当前位置
      SEEK_END  文件的末尾
    
    ftell : 返回给定流 stream 的当前文件位置。
    void testftell() {
        FILE *fp;
        int  len;
    
        fp = fopen("file.txt", "r");
        if (fp == NULL) {
            perror("open file error");
            exit(-1);
        }
        fseek(fp, 0, SEEK_END);
    
        len = ftell(fp);
    
        printf("file size :%d bytes\n", len);
        fclose(fp);
    }
    
    remove() :  删除文件
    
    void testremove(){
        int ret;
        FILE *fp;
        char filename[] = "file.txt";
    
        fp = fopen(filename, "w");
    
        fprintf(fp, "%s", "this is gorey");
        fclose(fp);
    
    
        ret = remove(filename);
    
        if (ret == 0)
            printf("delete ok\n");
        else
            printf("delete failed\n");
    }
    
    rename() 文件重命名
    void testrename(){
        int ret;
        char oldname[] = "file.txt";
        char newname[] = "newfile.txt";
        ret = rename(oldname, newname);
        if (ret == 0)
            printf("rename ok\n");
        else
            printf("rename failed : %s\n", strerror(errno));
    }
    
    rewind(): 重置到文件头
    
    setbuf() : 复制流中的内容到 buf 中
    void testsetbuf(){
        char buf[BUFSIZ];
        setbuf(stdout, buf);
        puts("this is gorey");
        fflush(stdout);
    
        printf("buf : %s\n", buf);
    }
    
    int setvbuf(FILE *stream, char *buffer, int mode, size_t size)
    tmpfile(); 创建一个临时文件, 关闭时, 自动删除文件
    tmpnam(): 指定名称的创建临时文件
    void testtmpname(){
        char buf[L_tmpnam];
        char *ptr;
    
        tmpnam(buf);
    
        printf("tmp file 1 name :%s\n", buf);
    
        ptr = tmpnam(NULL);
        printf("tmp file 2 name :%s\n", ptr);
    }
    fprintf 发到指定流
    printf 发到 stdout
    sprintf 发到 string
    vfprintf 格式化列表发送到 stream 里
    vprintf 格式化发送到 stdout
    vsprintf 格式化发送到 string
    snprintf 格式化字串到 str
    fscanf 从流中读取格式化输入
    scanf 从stdin
    sscanf 从字符串中读取
    fgetc 读一个字节
    fgets 读一行
    fputc 
    fputs
    getc
    getchar
    gets
    putc
    putchar
    puts
    ungetc : 反向推到流中, 下一个getc 就是这个元素
    perror 把错误信息输出到  stderr
    
    
    
    • stdlib.h
    size_t, wchar_t, div_t, ldiv_t
    NULL, EXIT_FAILURE, EXIT_SUCCESS
    exit(0);
    atof
    atoi
    atol
    strtod
    strtol
    strtoul
    calloc
    free
    malloc
    realloc
    abort
    atexit
    exit
    getenv
    system
    bsearch
    qsort
    abs
    div
    labs
    ldiv
    rand
    srand
    mblen
    mbstowcs
    mbtowc
    wcstombs
    wctomb
    
    • string.h
    memchr
    memcmp
    memcpy
    memmove
    memset
    strcat
    strncat
    strchr
    strcmp
    strncmp
    strcoll
    strcpy
    strncpy
    strcspn
    strerror
    strlen
    strpbrk
    strrchr
    strspn
    strstr
    strtok
    strxfrm
    
    • time.h
    struct tm {
       int tm_sec;         /* 秒,范围从 0 到 59        */
       int tm_min;         /* 分,范围从 0 到 59        */
       int tm_hour;        /* 小时,范围从 0 到 23        */
       int tm_mday;        /* 一月中的第几天,范围从 1 到 31    */
       int tm_mon;         /* 月,范围从 0 到 11        */
       int tm_year;        /* 自 1900 年起的年数        */
       int tm_wday;        /* 一周中的第几天,范围从 0 到 6    */
       int tm_yday;        /* 一年中的第几天,范围从 0 到 365    */
       int tm_isdst;       /* 夏令时                */
    };
    
    asctime 
    clock
    ctime
    difftime
    gmtime
    localtime
    mktime
    strftime
    time
    

    相关文章

      网友评论

        本文标题:linux c 标准库函数

        本文链接:https://www.haomeiwen.com/subject/wrsfoctx.html