IO库函数
文件读写权限 r(读),w(写),x(执行)
第一位表示文件类型, 剩下的 9 位划分成 3 位为一组,分别表示文件拥有者的权限,文件拥有者所在用户组的权限,其它用户的权限
open()
函数定义:#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>int open(const char *pathname, int flags);int open(const char *pathname, int flags, mode_t mode);参数含义:pathname: 路径和文件名,flags: 文件打开方式,可用多个标志位按位或设置,常用的标志位参数如下:O_CREAT 要打开的文件名不存在时自动创建改文件。O_EXCL 要和 O_CREAT 一起使用才能生效,如果文件存在则 open() 调用失败。O_RDONLY 只读模式打开文件。O_WRONLY 只写模式打开文件。O_RDWR 可读可写模式打开文件。O_APPEND 以追加模式打开文件。O_NONBLOCK 以非阻塞模式打开。mode:权限掩码,对不同用户和组设置可执行,读,写权限,使用八进制数表示,此参数可不写。返回值:open() 执行成功会返回 int 型文件描述符,出错时返回 -1 。
close()
#include <unistd.h>
int close(int fd);
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
int fd=open("./hello",O_CREAT|O_RDWR);//创建该文件并对当前用户设置可读写
if(-1 == fd)
{
perror("open");
return -1;
}
printf("文件描述符:%d\n",fd);
close(fd);
return 0;
}
read()
#include <unistd.h>ssize_t read(int fd, void *buf, size_t count);参数含义:fd : 要读的文件描述符buf : 缓冲区,存放读到的内容。count : 每次读取的字节数函数功能: 每次从 fd 读取 count 个字节,保存到 buf 中。返回值: 返回值大于 0 ,表示读取到的字节数;等于 0 在阻塞模式下表示到达文件末尾或没有数据可读(EOF),并调用阻塞;等于 -1 表示出错,在非阻塞模式下表示没有数据可读。
write()
#include <unistd.h>ssize_t write(int fd, const void *buf, size_t count);参数含义:fd: 文件描述符;buf: 缓存区,存放将要写入的数据;count : 每次写入的个数。函数功能:每次从 buf 缓存区拿 count 个字节写入 fd 文件。返回值: 大于或等于 0 表示执行成功,返回写入的字节数;返回 -1 代表出错。
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char* argv[])
{
if (argc != 3)
{
printf("Usage:%s,<src file><obj file>\n", argv[0]);
return -1;
}
int fd_src, fd_obj;
char buf[32] = { 0 };
ssize_t ret;
fd_src = open(argv[1], O_RDWR);
if (fd_src < 0)
{
printf("open is error\n");
return -1;
}
fd_obj = open(argv[2], O_RDWR | O_CREAT , 0666);
if (fd_obj < 0)
{
printf("open is error\n");
return -1;
}
while ((ret = read(fd_src, buf, 32)) != 0)
{
write(fd_obj, buf, ret);
}
close(fd_src);
close(fd_obj);
}
lseek()
tinclude <sys/types.h>
#include <unistd.h>
off_t lseek(int fd, off_t offset, int whence);
参数:
fd:文件描述符
whence:当前位置的基点,可以使用以下三组值
SEEK_SET:相对于文件开头
SEEK_CUR:相对于当前的文件读写指针位置
SEEK_END:相对于文件末尾
offset:偏移量,单位是字节的数量,可以正可负,如果是负值,表示向前移动,如果是正值,表示向后移动。
返回值:成功返回当前位移,失败返回-1。
mkdir()
#include <sys/stat.h>#include <sys/types.h>
int mkdir(const char *pathname, mode_t mode);返回值:0成功,-1失败
opendir()
打开指定的目录,并返回 DIR*形态的目录流
#include <sys/types.h>#include <dirent.h>DIR *opendir(const char *name);参数含义:name :路径名字。返回值:成功返回打开的目录流,失败返回 NULL 。
closedir()
关闭目录流。
#include <sys/types.h>#include <dirent.h>int closedir(DIR *dirp);参数含义: dirp :要关闭的目录流指针。
readdir()
#include <dirent.h>
struct dirent *readdir(DIR*dirp);返回值 dirent 结构体 成功,失败返回 NULL
struct 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 */ };
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
int main(int argc, char* argv[])
{
//步骤一:定义变量
int fd_src, fd_obj;
char buf[32] = { 0 };
char file_path[32] = {0};
char file_name[32] = { 0 };
ssize_t ret;
DIR* dp;
struct dirent* dir;
//均骤二;从键盘输入文件路径
printf("Please enter the file path:\n");
scanf("%s", file_path);
dp = opendir(file_path);
if (dp == NULL)
{
printf("opendir is error\n");
return -1;
}
printf("opendir is ok\n");
while (1)
{
dir = readdir(dp);
if (dir != NULL)
printf("file name is %s\n", dir->d_name);
else
break;
}
printf("Please enter the file name:\n");
scanf("%s", file_name);
fd_src = open(strcat(strcat(file_path,"/"), file_name), O_RDWR);
if (fd_src < 0)
{
printf("open is error\n");
return -1;
}
fd_obj = open(file_name,O_RDWR|O_CREAT,0666);
if(fd_obj<0)
{
printf( "open is error\n");return -1;
}
//步骤六:获得文件描述符
while ((ret = read(fd_src, buf, 32))!= 0)
{
write(fd_obj, buf, ret);
}
close(fd_src);
close(fd_obj);
closedir(dp);
return 0;
}
静态库
gcc -c mylib.c //生成目标文件
ar cr libmylib.a mylib.o //从目标文件生成静态库文件
gcc test.c -lmylib -L. //链接libmylib.a静态库,生成.out文件
动态库
gcc -c -fpic mylib.c //生成位置无关的目标文件
gcc -shared -o libmylib.so mylib.o //通过目标文件生成动态库
gcc test.c -lmylib -L. //使用动态库
//修改环境变量
//方法一,只对当前终端有效
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/joyer/souce
//方法二,直接添加到/lib文件夹,或者/usr/lib文件夹下面
//方法三,修改/etc/ld.so.conf文件,加入动态库的位置,然后使用ldconfig更新目录