strtokchar *strtok(char *str, const char *delim)功能字符串分割参数str要分割字符串的首地址delim分隔符返回值成功返回分隔到的字符串的首地址失败NULL使用文件IO实现文件的拷贝int copy_file(const char *srcname, const char *dstname) { int fdsrc open(srcname, O_RDONLY); int fddst open(dstname, O_WRONLY | O_CREAT | O_TRUNC, 0664); if (fdsrc 0 || fddst 0) { printf(open error\n); return -1; } char buff[1024] {0}; while (1) { ssize_t size read(fdsrc, buff, sizeof(buff)); if (size 0) //0 :文件末尾 0 :出错 { break; } write(fddst, buff, size); } close(fdsrc); close(fddst); return 0; }文件位置定位函数off_t lseek(int fd, off_t offset, int whence)功能文件读写位置偏移和定位参数fd需要重定位的文件offset偏移量字节whence要偏移的起始位置SEEK_SET :文件开头SEEK_CUR :文件当前读写位置SEEK_END 文件末尾返回值成功返回偏移后的位置到文件开头的偏移量失败-1求文件大小lenoff_t len lseek(fd, 0, SEEK_END);lseek(fd, 0, SEEK_SET); //复位文件IO和标准IO的区别文件IO标准IO面向文件描述符fd面向文件流指针FILE *由Linux内核提供属于系统调用跨平台可移植性弱C标准库函数跨平台可移植性强无缓冲区所以可以用在对硬件的控制上有缓冲区实现数据缓存避免频繁进行用户空间和内核空间切换带来的时间消耗可以操作普通文件也可以操作硬件相关的设备类文件管道套接字等主要使用在对普通文件-的读写上文本文件操作居多缓冲区定义高速设备和低速设备进行交互时为了匹配低速设备的速率需要在高速设备和低速设备之间增加一个缓冲区用于数据缓存。比如队列行缓冲1k字节 terminal主要用于人机交互--- stdout ---终端缓存区满或者遇到\n刷新行缓存多是关于终端的一些操作遇到 \n刷新缓存区满刷新程序结束刷新fflush刷新 fflush(stdout); //强制刷新全缓冲4k主要用于文件的读写 --- 普通文件的操作缓存区满刷新缓存区 4096对普通文件进行标准IO操作建立的缓存一般为全缓存刷新条件:缓存区满刷新程序结束刷新fflush来刷新 fflush(fp);文件关闭刷新无缓冲0k 主要用于出错处理信息的输出 stderr不对数据缓存直接刷新printf();stdoutfprintf(strerr,fopen error %s,filename);界面交互 出错处理出错处理相关函数errno定义全局的错误码变量程序运行过程中会将对应的函数调用出错信息保存在这个变量中strerrorchar *strerror(int errnum)功能将错误码装换成对应的错误信息以字符串方式返回参数errnum错误码返回值返回错误码对应的错误描述信息*perrorvoid perror(const char *s)功能打印出错信息和出错原因参数s自定义的错误信息errorvoid error(int status, int errnum, const char *format, ...)功能打印自定义的错误信息error(1, errno, %s : %s : %d :open error: aaa, __FILE__, __func__, __LINE__);参数status状态值0 SUCCESS1FAILerrnum错误码errnoformat格式化后错误信息字符串c语言内置宏__FILE__ 表示是那个文件__LINE__ 表示第几行__FUNC__ 表示在那个函数 // __FUNCTION____DATE__ 表示日期目录操作打开目录 opendir读目录 readdir关闭目录 closedirDIR *opendir(const char *name)功能打开一个目录并获得一个目录流指针参数name目录名返回值成功目录流指针失败NULLstruct dirent *readdir(DIR *dirp)功能读取目录中的文件信息参数dirp目录流指针返回值成功返回文件信息的结构体指针失败NULLstruct dirent { ino_t d_ino; /* Inode number */ off_t d_off; /* Not an offset; see below */ unsigned short d_reclen; /* Length of this record */ unsigned char d_type; /* Type of file; not supported by all filesystem types */ char d_name[256]; /* Null-terminated filename */ };int closedir(DIR *dirp)功能关闭一个目录流mkdirint mkdir(const char *pathname, mode_t mode)功能创建一个目录参数pathname目录名mode对目录的读写执行权限 0777返回值成功0失败-1char *getcwd(char *buf, size_t size)功能获取当前工作路径参数buf 存储当前路径的空间size空间大小返回值成功buf的首地址失败NULLint chdir(const char *path)功能修改当前工作路径参数path新的工作路径返回值成功0失败-1其他chmod 八进制值 文件名 --修改该文件的读写执行权限chmod 0777 1.txtpwd 获取当前目录对应的绝对路径时间相关函数timetime_t time(time_t *tloc)功能获取1970-1-1 000到现在的秒数参数tloc保存秒数的变量地址返回值返回秒数ctimechar *ctime(const time_t *timep)功能将秒数转换成字符串时间参数timep秒数的地址返回值返回时间字符串localtimestruct tm *localtime(const time_t *timep)功能将秒数转换成日历时间参数timep秒数的地址返回值返回具体时间的结构体指针struct tm { int tm_sec; /* Seconds (0-60) */ int tm_min; /* Minutes (0-59) */ int tm_hour; /* Hours (0-23) */ int tm_mday; /* Day of the month (1-31) */ int tm_mon; /* Month (0-11) */ int tm_year; /* Year - 1900 */ int tm_wday; /* Day of the week (0-6, Sunday 0) */ int tm_yday; /* Day in the year (0-365, 1 Jan 0) */ int tm_isdst; /* Daylight saving time */ };