文件编程
liunx系统API:
打开:open
读写:write/read
光标定位:lseek
关闭:close
open函数:
文件描述符:读写函数根据文件描述符进行后续的操作
文件描述符:索引的作用
open函数的返回值就是文件描述符
fd为正整数,文件存在,打开成功;为-1打开失败,不存在文件
函数原型
int open(const char *pathname, int flags);
参数说明:
pathname:要打开的文件名
flags:O_RDONLY 只读打开
O_WRONLY 只写打开
O_RDWR 可读可写打开
这三个权限只能指定一个
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
//int open(const char *pathname, int flags);
int main()
{
int fd;//文件描述符,后续的读写根据文件描述符操作
fd=open("/file1",O_RDWR);//用只读只写的方式打开文件,文件描述符是open函数的返回值
printf("fd=%d\n",fd);//文件存在,文件描述符是正整数,否则是负数
return 0;
}
可选择的参数:
O_CREAT:文件不存在,创建,需要第三个参数mode,说明该文件的存取许可权限
O_EXCL:同时指定了O_CREAT,文件已经存在,打开文件失败,返回-1;
O_TRUNC:打开文件,若文件本来有内容,只读或只写打开,把内容全部干掉。
O_APPEND:每次写都加到文件的尾端
#include<stdio.h>
#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);
int main()
{
int fd;//文件描述符
fd=open("./file1",O_RDWR);//只读只写的方式打开文件
if(fd==-1){//如果返回值是负数,文件不存在,打开失败
printf("open file fail\n");
fd=open("./file1",O_RDWR|O_CREAT,0600);//文件不存在创建文件,文件的权限是0600(可读可写)
if(fd>0){//如果返回值是正整数,文件存在,打开成功
printf("open file success\n");
}
}
printf("open success:fd=%d\n",fd);
return 0;
}
#include<stdio.h>
#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);
int main()
{
int fd;//文件描述符
fd=open("./file1",O_RDWR|O_CREAT|O_EXCL,0600);//文件不存在创建文件,文件的权限是0600(可读可写),指定了O_CREAT,文件已经存在,打开文件失败,返回-1;
if(fd==-1){
printf("file1 cunzai\n");
}
return 0;
}
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include<string.h>
#include<stdlib.h>
// int open(const char *pathname, int flags);
// int open(const char *pathname, int flags, mode_t mode);
// ssize_t write(int fd, const void *buf, size_t count);
// 含义:将缓冲区buf指针指向内存的数据,写count个字节,写进fd,fd是打开文件的文件描述符。
// 成功写入返回写入的个数,返回的值是正整数。
//ssize_t read(int fd, void *buf, size_t count);
// 含义:从fd这个文件读取count个字节到缓冲区buf指针指向的内存
int main()
{
int fd;//文件描述符
int n_write;//写入文件内容的个数
char *buf="chenlichen hen shaui!";//write函数缓冲区存放的内容
fd=open("./file1",O_RDWR|O_APPEND);//只读只写的方式打开文件,把内容写进尾端
n_write=write(fd,buf,strlen(buf));//把内容写入文件
if(n_write!=-1){//返回值不是负数,为正整数,写入文件成功
printf("write %d byte to file\n",n_write);
}
close(fd);//关闭文件
return 0;
}
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include<string.h>
#include<stdlib.h>
// int open(const char *pathname, int flags);
// int open(const char *pathname, int flags, mode_t mode);
// ssize_t write(int fd, const void *buf, size_t count);
// 含义:将缓冲区buf指针指向内存的数据,写count个字节,写进fd,fd是打开文件的文件描述符。
// 成功写入返回写入的个数,返回的值是正整数。
//ssize_t read(int fd, void *buf, size_t count);
// 含义:从fd这个文件读取count个字节到缓冲区buf指针指向的内存
int main()
{
int fd;//文件描述符
int n_write;//写入文件内容的个数
char *buf="test";//write函数缓冲区存放的内容
fd=open("./file1",O_RDWR|O_TRUNC);//只读只写的方式打开文件,把原来的内容截断为0
n_write=write(fd,buf,strlen(buf));//把内容写入文件
if(n_write!=-1){//返回值不是负数,为正整数,写入文件成功
printf("write %d byte to file\n",n_write);
}
close(fd);//关闭文件
return 0;
}