C Function - smilesum/C-CPlus-Language- GitHub Wiki

unlink

int unlink(const char* pathname)

将pathname指定的文件的链接数-1,但并不会直接删除文件,由操作系统根据文件的链接数以及是否有进程仍打开该文件来决定是否删除。 举例:

  1. 用户fopen新建temp文件
  2. ls 可以查看到temp文件
  3. 用户unlink该文件
  4. ls无法查看到文件
  5. 用户进程仍然可以往文件中写入数据,证实文件并未被关闭

demo:http://blog.chinaunix.net/uid-28852942-id-3688903.html

offsetof

size_t offsetof(structName, memberName)

返回结构体成员在结构体中的偏移字节数 demo:http://www.runoob.com/cprogramming/c-macro-offsetof.html

WIFEXITED

int WIFEXITED(pid_t status)

判断程序是否正常退出,正常退出返回非零值

__NR_gettid

uint32_t thread_id=syscall(_NR_gettid)

获取线程id

strtoul

unsigned long strtoul(const char* nptr, char** endptr, int base)

将nptr代表的数字字符串方按base进制到十进制进行转换

strchr

char* strchr(const char *s, char *c) : 查找c在s中第一次出现的位置

strcspn

size_t strcspn(const char *s, const char *reject): 从s开头的连续且没有任意一个字符属于reject的,返回这个子字符串长度

strspn

size_t strspn(const char *s, const char *accept): 与strcspn相反,返回s开始的且均在accept中的字符串长度

strpbrk

char *strpbrk(const char *s1, const char *s2): 查找s1中第一个含有s2中任意字符串的位置,并返回

strtok

char *strtok(char s[], const char* delim)

源码

    char *s;        
    
    const char *delim; 
 
{

    static char *lasts;

    register int ch;

    if (s == 0)
	s = lasts;
    do {
	if ((ch = *s++) == '\0')
	    return 0;
    } while (strchr(delim, ch));
    --s;
    lasts = s + strcspn(s, delim);
    if (*lasts != 0)
	*lasts++ = 0;
    return s;
}

使用: 1)一维分割:

int in=0;

char buffer[]="a,b,c";

char *p[3];

char *buf=buffer;

while((p[in++]=strtok(buf, ","))!=NULL) {

     buf=NULL;

}
  1. 二维分割,对于二维情况使用strtok会将lasts破坏
/*  
 * strtok_r.c:  
 * Implementation of strtok_r for systems which don't have it.  
 *  
 * This is taken from the GNU C library and is distributed under the terms of  
 * the LGPL. See copyright notice below.  
 *  
 */   
   
#ifdef HAVE_CONFIG_H    
#include "configuration.h"    
#endif /* HAVE_CONFIG_H */    
   
#ifndef HAVE_STRTOK_R    
   
static const char rcsid[] = "$Id: strtok_r.c,v 1.1 2001/04/24 14:25:34 chris Exp $";   
   
#include <string.h>    
   
#undef strtok_r    
   
/* Parse S into tokens separated by characters in DELIM.  
   If S is NULL, the saved pointer in SAVE_PTR is used as  
   the next starting point.  For example:  
        char s[] = "-abc-=-def";  
        char *sp;  
        x = strtok_r(s, "-", &sp);      // x = "abc", sp = "=-def"  
        x = strtok_r(NULL, "-=", &sp);  // x = "def", sp = NULL  
        x = strtok_r(NULL, "=", &sp);   // x = NULL  
                // s = "abc/0-def/0"  
*/   
char *strtok_r(char *s, const char *delim, char **save_ptr) {   
    char *token;   
   
    if (s == NULL) s = *save_ptr;   
   
    /* Scan leading delimiters.  */   
    s += strspn(s, delim);   
    if (*s == '/0')    
        return NULL;   
   
    /* Find the end of the token.  */   
    token = s;   
    s = strpbrk(token, delim);   
    if (s == NULL)   
        /* This token finishes the string.  */   
        *save_ptr = strchr(token, '/0');   
    else {   
        /* Terminate the token and make *SAVE_PTR point past it.  */   
        *s = '/0';   
        *save_ptr = s + 1;   
    }   
   
    return token;   
}  

源码:https://blog.csdn.net/sjin_1314/article/details/8242098 demo:https://www.jb51.net/article/43744.htm

sscanf

int sscanf(const char *str, const char *format, arg)
//demo
sscanf(buff, "%s", name)

将str的字符串以format格式写入arg

opendir

DIR* opendir(const char *path) 获取path子目录下的所有文件和目录列表,如果path为文件则返回NULL

readdir

struct dirent* readdir(DIR* dir_handle) 循环读取opendir返回的值,文件和目录都会读取 返回值:

struct dirent
{
  long d_ino; /* inode number 索引节点号 */
  off_t d_off; /* offset to this dirent 在目录文件中的偏移 */
  unsigned short d_reclen; /* length of this d_name 文件名长 */
  unsigned char d_type; /* the type of d_name 文件类型 */
  char d_name [NAME_MAX+1]; /* file name (null-terminated) 文件名,最长255字符 */
}

strncmp

int strncmp(const char *str1, const char *str2, int n)
比较str1与str2的前n个字符

gettimeofday

int gettimeofday(struct timeval *tv, struct timezone *tz)
获取当前的时间,存入tv,时区信息存入tz
_STRUCT_TIMEVAL
{
	__darwin_time_t	        tv_sec;	        /* seconds */
	__darwin_suseconds_t    tv_usec;        /* and microseconds */
};
为从计算机元年开始到现在的秒和微妙

random_shuffle

void random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
将从_first到_last-1的数据随机打乱

va_start

用于获取参数

#include <stdio.h>
#include <stdarg.h>
 
int add_nums(int count, ...) 
{
    int result = 0;
    va_list args;
    va_start(args, count);
    for (int i = 0; i < count; ++i) {
        result += va_arg(args, int);
    }
    va_end(args);
    return result;
}
 
int main(void) 
{
    printf("%d\n", add_nums(4, 25, 25, 50, 50));
}
//150

snprintf

snprintf(char *buffer, int n, char *format, ...)
//将...的参数按照format格式写入buffer,最多写入n个
//返回值:...的长度

char buffer[256];
//返回值ret = 13,buffer中的内容为123456789
int ret = snprintf(buffer, 10, "%s", "1234567890abc");
memset(buffer, 0x0, sizeof(buffer));
//返回值ret = 3,buffer中的内容为123
ret =snprintf(buffer, 10, "%s", "123");

vsnprintf

与snprintf一样,只是将... 参数换成了va_list

int sprintf(char* buffer, const char* format, ...); 
int vsprintf(char* buffer, const char* format, va_list ap);

fcntl

根据文件描述符来操作文件特性

int fcntl(int fd, int cmd)
//cmd:
//F_GETFL:取得fd的文件状态标志

setsockopt

int setsockopt(int s, int level, int optname, const void *optval, socklen_t optlen)
//s套接字描述符
//level: 预设置的网络层
//optval:设置的值
//optlen:optval长度
//参考:http://c.biancheng.net/cpp/html/374.html
//optname:可能参数如下
//SO_SNDBUF:设置发送缓存区大小
//TCP_NODELAY:是否将包合并ack
//SO_KEEPALⅣE:是否保持长连接
//SO_SNDTIMEO:发送超时

getaddrinfo

int getaddrinfo(const char *nodename, const char *servname, struct addrinfo *hints, struct addrinfo **res)
//根据hints的要求,将nodename的地址信息解析到res。
⚠️ **GitHub.com Fallback** ⚠️