zoukankan      html  css  js  c++  java
  • C语言笔记整理(2)

    字符串和字符串函数

     

    char *gets(char *buf);

    读取字符串直到\n, 然后添加\0, gets读取\n并丢弃.返回指针和参数指针相同.如果出错或者遇到文件尾,则返回NULL

     

    int puts(const char *);

    输出直到遇到\0,自动添加\n

     

     

    char *fgets(char* buf, int maxCount, FILE *file);

    读取maxCount-1或者到换行符, 然后添加\0, fgets读取\n但不丢弃.

     

    int fputs(const char *str, FILE *file);

    file也可以为stdoutfputs并不为输出自动添加\n

     

    size_t strlen(const char *str); //长度,算到\0为止,不包括\0

     

    char *strcat(char *dest, const char *source); //append

    返回第一个参数指针

    char *strncat(char *dest, const char *source, size_t Count);

     

    int strcmp(const char *str1, const char *str2); //比较

    int strncmp(const char *str1, const char *str2,  size_t maxCount);

     

    char* strcpy(char *dest, const char *source); //复制

    char* strncpy(char *dest, const char *source, size_t count);

     

    char *strchr(const char *str, int c);

    返回指向strc的第一次出现位置的指针,如果没有找到,返回NULL

     

    char *strpbrk(const char *str1, const char *str2);

    返回指向strstr2中任何字符的第一个位置的指针,如果没有找到,返回NULL

     

    char *strrchr(const char *str, int c);

    返回指向strc的最后一次出现位置的指针,如果没有找到,返回NULL(reverse strchr)

     

    int atoi(const char *str);

    doubleatof(const char *str);

    long atol(const char *str);

     

    long strtol(const char *str, char **pend, int base);

    pend: 指向最后一个字符的指针

    base:转换的基数,从1036(z)

    char *end = NULL;

    long value = strtol("1a1z", &end, 16); //417 end指针指向z

     

    char *itoa(int val, char *buf, int radix);

    返回指针为buf指针.radix为进制

    char buf[32] = {0};

    itoa(256, buf, 16); //buf = “100”

     

    存储类,链接和内存管理

     

    int a = 10; //具有外部链接的静态存储类,文件作用域,函数外声明,只可用常量初始化

    //在其余文件中使用extern时可以使用,各个文件定义的全局变量名不可以相同.在编译时期初始化,必须初始化,如果未初始化,字节设为0.如果其他文件中使用可以

    //使用extern int a声明.

    static int b = 1; //具有内部链接的静态存储类,文件作用域,函数外声明,只可被同一文件内函数使用, 在编译时期初始化,必须初始化,如果未初始化,字节设为0

    int main(int argc, _TCHAR* argv[])

    {

    retister int quick; //寄存器存储类,空连接,代码块作用域,函数内声明,变量存储在寄存器中以便快速存取,所以你不能获得变量地址.

             static int c = 10; //空连接的静态存储类,代码块作用于,函数内声明.

    在编译时期初始化,必须初始化,如果未初始化,字节设为0

    char buf[32] = {0}; //自动存储类,空连接,代码块作用域,函数内声明, 相当于auto char buf[32] = {0}

             return 0;

    }

     

    所谓静态指变量的位置不动.

     

    随机函数: int rand(void);

    malloc()free()

    double *pd = (double *)malloc(30*sizeof(double));

    if (pd != NULL)

    {

             free(pd);

    }

    double *pd = (double *)calloc(30, sizeof(double)); //块中的全部位置设为0

     

    变长数组的分配:

    int n = 2;

    int m = 3;

    int (*p)[m] = (int (*)[m])malloc(n*m*sizeof(int));

     

    类型限定词volatile

    volatile告诉编译器该变量除了可被程序修改以外还可被其它代理改变,典型地,它被用于硬件地址和其它并行运行的程序共享的数据.

    int x = 1;

    int a = x;

    int b = x;

    编译其可能注意到两次使用了x但是未改变x的值,所以可能把x临时存储在一个寄存器中以快速存取,这个过程被成为缓存,如果两个语句践其他代理改变了x,如果没有规定volatile关键字,编译器无法得知这种改变是否发生,

     

    类型限定词restrict

    restrict表明指针是访问数据对象的唯一且初始方式.

    int ar[10];

    int * restrict restar = (int *)malloc(10*sizeof(int));

    int *par = ar;

    for (n = 0; n < 10; ++n)

    {

             par[n] += 5;

             restart[n] += 5;

             ar[n] += 2;

             par[n] += 3;

             restart[n] += 3;

    }

    编译器可以优化代码restart[n] += 5restart[n] += 3restart[n] += 8;

    而不可以优化代码par [n] += 5par [n] += 3par [n] += 8;因为两个语句之间ar[n] += 2;此时值已经改变,所以如果使用restrict,编译器可以确定函数内没有其他标志符修改指针指向的数据,放心地优化代码.

     

    C99标准下:

    void *memcpy(void *restrict s1, const void* restrict s1, size_t n);

    //memcpy不允许内存重叠,所以使用restrict要求不能访问同一数据块.

    void *memmove(void *s1, const void *s2, size_t n);

    //memmove允许重叠.复制s1到临时内存再到s2

     

     

    文件输入/输出

    ANSI C中:

    二进制模式:\r\n换行,MS-DOS中,使用ctrl+z标识文件结尾,C语言以二进制模式打开文件会把ctrl+z当作文件的一个字符,可以看到\r\n

    文本模式:\n换行,MS-DOS中,C语言以文本模式打开文件,可以将ctrl+z当作文件结尾字符,将\r\n看作\n

     

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

    不成功返回NULL

    mode:

    r        读 

    write          文件不存在则创建,文件长度截为0 

    a        可写文件不存在则创建,存在则向文件尾部追加内容

    r+      可读写,可立即更新 

    w+     文件不存在则创建,文件长度截为0,可立即更新 

    a        可读写,文件不存在则创建,存在则向文件尾部追加内容,可立即更新 

    b        可与上面几个配套,以二进制模式打开而非文本模式打开.

    对于linuxunix,带b字母的模式和不带b字母模式相同.

     

    int fclose(FILE *file);

    成功返回0,否则返回EOP

     

    一下file可用stdinstdout, stderr,向屏幕输入和输入.

    int getc(FILE *file)

    到达文件尾时返回EOF

     

    int putc(int c, FILE *file);

    putc(c, stdout)putchar(c)作用一样.

     

    int fscanf(FILE *file, const char *format,...);

    int fprintf(FILE *file, const char *format,...);

     

    char *fgets(char* buf, int maxCount, FILE *file);

    读取maxCount-1或者到换行符, 然后添加\0, fgets读取\n但不丢弃.

     

    int fputs(const char *str, FILE *file);

    file也可以为stdoutfputs并不为输出自动添加\n

    char line[10] = {0};

    while((fgets(line, 10, stdin) != NULL) && line[0] != '\n'))

    {

             fputs(line, stdout);

    }

    123

    123

    1234567890123456

    1234567890123456 //第一次得到9个字符,但是其余字符仍在输入队列中,下一次从第10个字符开始读起,直到读到\n,而fputs因为屏幕缓冲区直到\n才输出.所以16个字符一起输出.

     

    int fseek(File *file, long offset, int origin);

    定位到从origin开始移动offset的位置,正常返回0,错误返回-

    orgin:

    SEEK_SET 文件开始

    SEEK_CUR 当前位置

    SEEK_END 文件结尾

     

    long ftell(FILE *file);

    返回文件的当前位置(距离文件开始出的字节数目)

    fseekftell只能限制文件的大小在long类型的表示范围之内.

     

    fsetposfgetpos文件大小无限制.但是好像不能任意定位啊.

    int fsetpos(FILE *file, const fpos_t *pos);

    fpos_t 应该是调用fgetpos()获取的,成功返回0,错误返回非0

     

    int fgetpos(FILE *file, fpos_t * restrictpos);

    函数在pos所指的位置防止一个fpos_t值,描述文件中的一个位置,成功返回0,错误返回非0

    fpos_t position;

    FILE *pFile = fopen ("myfile.txt","w");

    fgetpos (pFile, &position);

    fputs ("That is a sample",pFile);

    fsetpos (pFile, &position);

    fputs ("This",pFile); //myfile.txt will contain: This is a sample

    fclose (pFile);

     

    int ungetc(int c, FILE *file);

    c放回file

     

    int setvbuf(FILE *file, char *buf, int mode, size_t size);

    建议一个供标准I/O函数使用的替换缓冲区,成功返回0,失败返回非0

    设置用于stream()的缓冲区到buf (缓冲区),其大小为size;

    Mode:

    _IOLBF 行缓冲

    _IOFBF 完全缓冲

    _IONBF 无缓冲

     

    二进制I/O(将数据存储在一个使用域程序具有相同表示方法的文件中,就称数据以二进制存储,中间没有数字形式到字符串形式的转换)

    char* fread(void *buf, size_t size, size_t count, File *file);

    double buf[256] = {0};

    fread(buf, sizeof(double), 256, fp);

     

    size_t fwrite(void * restrict str, size_t size, size_t count, File * restrict file);

    size: 以字节为单位

    char buf[256] = {0};

    fwrite(buf, 256, 1, fp); //或者fwrite(buf, sizeof(char), 256, fp);

     

    int feof(FILE *file);

    如果最近一次输入调用检测到文件结尾,返回非0,否则返回0

    int ferror(FILE *file);

    如果发生读写错误,返回非0,否则返回0

    EOF表示到文件尾也表示读入错误.

  • 相关阅读:
    利用JS判断浏览器种类
    Navicat for MySQL导出表结构脚本的方法
    Spring中Quartz的配置及corn表达式
    easyUI中点击datagrid列标题排序
    JAVA中科学计数法转换普通计数法
    MySQL查询结果复制到新表(更新、插入)
    SVN错误:Attempted to lock an already-locked dir的解决
    TMS320VC5509的外部中断
    TMS320VC5509总线驱动LED灯
    TMS320VC5509的USB口通信
  • 原文地址:https://www.cnblogs.com/zengyou/p/2195570.html
Copyright © 2011-2022 走看看