c中基础函数用法.md 5.2 KB

c中基础函数用法

setsockopt 设置套接字选项

第二个参数level是被设置的选项的级别,如果想要在套接字级别上设置选项,就必须把level设置为 SOL_SOCKET。

int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen);

memset 设置对象内存

void *memset(void *s, int ch, size_t n);

作用

En
Copies the value static_cast<unsigned char>(ch) into each of the first count characters of the object pointed to by dest. If the object is a potentially-overlapping subobject or is not TriviallyCopyable (e.g., scalar, C-compatible struct, or an array of trivially copyable type), the behavior is undefined. If count is greater than the size of the object pointed to by dest, the behavior is undefined.
Ch
将ch的值复制到dest指向的对象的前count个字符中。如果对象是潜在重叠的子对象或不是TriviallyCopyable(例如,标量,C兼容的结构或平凡可复制的类型的数组),则行为是未定义的。如果count大于dest指向的对象的大小,则行为是未定义的。

复制ch的值到 s 指向的对象的前 n 个字符中。

语法

void *memset(void *s, int ch, size_t n);

参数

  • s: 指向要填充的内存块的指针
  • ch: 要被设置的值
  • n: 要被设置的内存块的大小,以字节为单位

返回值

  • 返回一个指向内存块 s 的指针。

    snprintf 函数

    作用

    En Writes formatted data from variable argument list to sized buffer. Ch 将格式化的数据从变量参数列表写入大小缓冲区。

语法

int snprintf( char *buffer, size_t buf_size, const char *format, ... );//可变参数

参数

  • buffer: 指向用于存储输出结果的缓冲区的指针。
  • buf_size: 缓冲区的最大长度。
  • format: 格式字符串,包含要写入缓冲区的文本。它可以包含以下占位符:
    • %s: 字符串
    • %c: 单个字符
    • %d: 十进制整数
    • %f: 浮点数
    • %x: 十六进制整数

memcpy 拷贝对象内存

作用

En
Copies count bytes from the object pointed to by src to the object pointed to by dest.
Both objects are reinterpreted as arrays of unsigned char.
If the objects overlap, the behavior is undefined.
If either dest or src is a potentially-overlapping subobject
(e.g., a field in a C-compatible struct) or is not TriviallyCopyable
(e.g., scalar, C-compatible struct, or an array thereof),
the behavior is undefined. If count is zero, the function does nothing.
Ch
将src指向的对象的count个字节复制到dest指向的对象。
两个对象都被重新解释为无符号字符数组。
如果对象重叠,则行为是未定义的。
如果dest或src是潜在重叠的子对象(例如,C兼容结构中的字段)
或不是TriviallyCopyable(例如,标量,C兼容结构或其数组),
则行为是未定义的。如果count为零,则函数不执行任何操作。

语法

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

参数

  • dest: 指向目标对象的指针
  • src: 指向源对象的指针
  • count: 要被复制的字节数

实现

void *memcpy(void *dest, const void *src, size_t count)
{
    char *d = dest;
    const char *s = src;
    while (count--)
        *d++ = *s++;
    return dest;
}

fread 读取文件

作用

En
Reads an array of count elements,
each one with a size of size bytes,
from the stream and stores them in the block of memory specified by ptr. Ch
从流中读取一个大小为size字节的count个元素的数组,并将它们存储在ptr指定的内存块中。

语法

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

参数

  • ptr: 指向要读取的元素存储位置的指针
  • size: 每个元素的大小(以字节为单位)
  • count: 元素的个数
  • stream: 指向 FILE 对象的指针,该 FILE 对象指定了一个输入流

fseek 移动文件指针

作用

En
Sets the position indicator associated with the stream to a new position.
The end-of-file internal indicator of the stream is cleared after a successful call to this function,
and all effects from previous calls to ungetc on this stream are dropped.
Ch
将与流关联的位置指示器设置为新位置。
成功调用此函数后,流的文件结束内部指示器将被清除,并且此流上先前调用ungetc的所有效果都将被丢弃。

语法

int fseek(FILE *stream, long int offset, int whence);

参数

  • stream: 指向 FILE 对象的指针,该 FILE 对象标识了流
  • offset: 新的文件位置,以字节为单位(long int 类型)
  • whence: 指定从哪里开始偏移
    • SEEK_SET: 文件开头
    • SEEK_CUR: 当前位置
    • SEEK_END: 文件结尾
    • 也可以使用数字 0, 1, 2 来代替 SEEK_SET, SEEK_CUR, SEEK_END
    • SEEK_SET: 0
    • SEEK_CUR: 1
    • SEEK_END: 2
  • 返回值: 成功返回0,失败返回非0值

注意事项

  • 该函数会清除之前调用ungetc函数的效果
  • 该函数会清除之前调用fseek函数的效果
  • 该函数会清除之前调用fsetpos函数的效果
  • 该函数会清除之前调用rewind函数的效果