int open(const char *pathname, int flags, mode_t mode);
flags :
O_CREAT :创建文件
O_EXCL :必须和O_CREAT一起使用,表示如果文件不存在就创建,如果文件存在就失败
O_APPEND:追加
O_RDWR
O_RDONLY
O_RWONLY
注意:
创建文件时,权限会受到umask的影响
实际创建文件权限 mode = mode & ~umask
如果创建的文件不想被umask影响,可以通过系统提供的umask函数来更改
// 更改umask值
mode_t umask(mode_t mask);
注释:当设置新 mask值时,则会返回旧mask值
…
代码:
#include
#include
#include
#include
extern int errno;
#define ERR_EXIT(msg) \
do { \
fprintf(stderr, "[%s][%d] %s : %s\n",__FILE__,__LINE__,\
msg,strerror(errno)); \
exit(EXIT_FAILURE); \
}while ( 0 )
int main( void )
{
int fd = -1;
/*设置新mask值为 0 并返回旧 mask 值*/
mode_t oldmask = umask(0);
if (-1 == (fd=open("b.txt", O_RDWR|O_CREAT|O_EXCL,0777)) )
{
if ( errno == EEXIST ) {
printf("文件已经存在,只能打开!\n");
if ( -1 == (fd=open("b.txt", O_RDWR)) )
ERR_EXIT("open");
} else
ERR_EXIT("open");
}
/*设置旧mask值*/
umask(oldmask);
printf("create file ok\n");
close(fd);
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
extern int errno;
#define LOG_ERROR(msg) \
do { \
fprintf(stderr, "[%s][%d] %s : %s\n",basename(__FILE__),__LINE__,\
msg,strerror(errno)); \
exit(EXIT_FAILURE); \
}while ( 0 )
#define LOG_INFO(msg) \
do { \
fprintf(stdout, "[%s][%d] %s : %s\n",basename(__FILE__),__LINE__,\
msg,strerror(errno)); \
}while(0)
int main()
{
int fd,ret;
char *buf = "123456789";
char rbuf[10]={0};
mode_t oldmask = umask(0);
fd = open("test.txt", O_RDWR|O_CREAT|O_EXCL, 0777);
if ( fd == -1 )
{
if (errno == EEXIST)
{
LOG_INFO("file already exists, can only be opened");
fd = open("test.txt", O_RDWR);
if (fd < 0)
{
LOG_ERROR("dst file open");
}
}
else
{
LOG_ERROR("dst file open");
}
}
umask(oldmask);
#if 0
ret = write(fd, buf, strlen(buf));
if (ret < 0)
{
LOG_ERROR("write data error");
}
#endif
/*file offset*/
/*
file data: 123456789
*/
/*offset = file head*/
ret = lseek(fd, 0, SEEK_SET);
printf("ret0 = %d\n",ret);
read(fd, rbuf, 1);
printf("%d : rbuf = %s\n",ret, rbuf);
/*offset = file head + 2 */
ret = lseek(fd, 2, SEEK_SET);
printf("ret1 = %d\n",ret);
read(fd, rbuf, 1);
printf("%d : rbuf = %s\n",ret, rbuf);
memset(rbuf, 0x00, sizeof(rbuf));
/*offset = file head*/
ret = lseek(fd, 0, SEEK_SET);
printf("ret2 = %d\n",ret);
/*偏移至文件头 + 2字节的位置*/
ret = lseek(fd, 2, SEEK_SET);
printf("ret3 = %d\n",ret);
/*offset = file end 偏移到文件尾,计算文件长度*/
ret = lseek(fd, 0, SEEK_END);
printf("file_size = %d\n",ret);
read(fd, rbuf, 1);
printf("%d : rbuf = %s\n",ret, rbuf);
/*偏移至文件尾 + 3字节的位置*/
ret = lseek(fd, 3, SEEK_END);
printf("ret4 = %d\n",ret);
/*偏移至当前位置 + 2字节的位置*/
ret = lseek(fd, 2, SEEK_CUR);
printf("ret5 = %d\n",ret);
write(fd, "abc", 3);
/*偏移至文件尾 - 3字节的位置*/
ret = lseek(fd, -3, SEEK_END);
printf("ret4 = %d\n",ret);
read(fd, rbuf, 1);
printf("%d : rbuf = %s\n",ret, rbuf);
return 0;
}
FILE *fopen(const char *path, const char *mode);
mode 参数类型:
r Open text file for reading. The stream is positioned at the beginning
of the file.
r+ Open for reading and writing. The stream is positioned at the beginning
of the file.
w Truncate file to zero length or create text file for writing. The
stream is positioned at the beginning of the file.
w+ Open for reading and writing. The file is created if it does not exist,
otherwise it is truncated. The stream is positioned at the beginning of
the file.
a Open for appending (writing at end of file). The file is created if it
does not exist. The stream is positioned at the end of the file.
a+ Open for reading and appending (writing at end of file). The file is
created if it does not exist. The initial file position for reading is
at the beginning of the file, but output is always appended to the end
of the file.
代码实例:
#include
#include
#include
#include
#include
#include
extern int errno;
#define ERR_EXIT(msg) \
do { \
fprintf(stderr, "[%s][%d] %s : %s\n",__FILE__,__LINE__,\
msg,strerror(errno)); \
exit(EXIT_FAILURE); \
}while ( 0 )
int main( int argc, char *argv[] )
{
FILE *fp = fopen("test.txt","w+");
char buf[1024]={};
//无缓存
// if(setvbuf(fp,buf,_IONBF,1024)!=0)
//行缓存
if(setvbuf(fp,buf,_IOLBF,1024)!=0)
//全缓存
// if(setvbuf(fp,buf,_IOFBF,1024)!=0)
ERR_EXIT("setvbuf");
fprintf(fp,"this\nis");
printf("buf=%s\n",buf);
/*偏移至文件尾*/
fseeko(fp,0,SEEK_END);
printf("%d\n",ftello(fp));
getchar();
fclose(fp);
}
运行结果:
4. int fileno(FILE *stream)
注释:可将fopen打开的文件流转换尾open打开的文件描述符
5. FILE *fdopen(int fd, const char *mode)
注释:可将open打开的文件描述符转换尾fopen打开的文件流类型
注意:一般情况下fopen和open不混用