您的位置:首页 > 运维架构 > Linux

Linux学习入门--文件编程学习一

2018-02-28 00:00 471 查看
摘要: 摘要: 《精通Linux C编程》学习心酸历程之文件编程
主要学习的函数为:
open()
creat()
close()
read() write()
access()

1. 文件打开 open()

2. 文件创建 open() creat() 注意creat不能创建设备文件,创建特殊文件用mknod,暂时标注,以后遇到再说

3. 文件关闭 close()

4. 文件读写 read() write()

5. 测试文件存在及文件权限等 access()

具体的函数说明可以参考man手册,此处不再累赘,或者参考书籍《精通Linux C编程 》第三章

简单的例子:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

int main(void)
{
int fd = 0;
size_t count = 1024;
char * buf = "This is file operator test!";
char buf_read[1024]= {0};

#if 1
/* 创建一个新的文件, 注意第三个参数mode,应该用8进制数:0xxx, 开始使用的是777
出现后面再次打开Permission denied 推荐使用参数符号S_IRWXU|S_IRWXG|S_IRWXO*/
fd = open("./file_test.txt",O_RDWR|O_CREAT|O_EXCL,0777);

if (fd == -1)
{
printf("File already exists, create failed!\r\n");
perror("open");

return 0;
}
else
{
printf("OK: open file successed!\r\n");
}

/* 将buf中的字符写入文件中 */
ssize_t num = write(fd, buf, strlen(buf));

printf("%ld", num);

/*关闭文件*/
if(close(fd) == 0)
{
printf("File close successfully!\r\n");
}
else
{
printf("File close failed!\r\n");
}
#endif

/* 再次打开文件 以可读可写的方式打开 */
int fd1 = open("./file_test.txt",O_RDWR);

if (fd1 == -1)
{
perror("open");
}
do
{
num = read(fd1,buf_read,100);

}while (num != 0 );

printf ("%s", buf_read);

/* 关闭文件 */
if(close(fd1) == 0)
{
printf("File close successfully!\r\n");
}
else
{
printf("File close failed!\r\n");
}
return 0;
}

结果:



当再次执行./file_test时,程序报错如下:



为了解决这一问题,提供以下两种方法:

第一种: creat()函数 :每次发现文件存在时,就会被截断为0,释放以前是数据 或者 open()去除O_EXCL选项, 如果是截断为0 O_APPEND如何操作??

fd = open("./file_test.txt",O_RDWR|O_CREAT,0777);

或者

fd = creat("./file_test.txt",0777);

Tips:

int creat(const char *pathname, mode_t mode); 等同于

int open(const char *pathname, OWRONLY|O_CREAT|O_TRUNC, mode_t mode);

第二种:用函数access(),此函数可以测试一个文件是否存在,还可以测试文件的权限,修改后的代码(见★)可以执行O_APPEND操作。

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

int main(void)
{
int fd = 0;
//int num;
size_t count = 1024;

char * buf = "This is file operator test!";
char buf_read[1024]= {0};
#if 1
if (access("./file_test.txt",F_OK) == -1)  //★新增代码
{
fd = open("./file_test.txt",O_RDWR|O_CREAT|O_EXCL,0777);

if (fd == -1)
{
printf("File already exists, create failed!\r\n");
perror("open");

return 0;
}
else
{
printf("OK: open file successed!\r\n");
}
}
else
{
fd = open("./file_test.txt",O_RDWR);  //★新增代码
}

ssize_t num = write(fd, buf, strlen(buf));

printf("%ld\n", num);

if(close(fd) == 0)
{
printf("File close successfully!\r\n");
}
else
{
printf("File close failed!\r\n");
}
#endif
ssize_t num_read ;

int fd1 = open("./file_test.txt",O_RDWR);
if (fd1 == -1)
{
perror("open");
}
do
{
num_read = read(fd1,buf_read,100);

}while (num_read != 0 );

printf ("%s\n", buf_read);

if(close(fd1) == 0)
{
printf("File close successfully!\r\n");
}
else
{
printf("File close failed!\r\n");
}
return 0;
}

今天学习到此为止,明天继续文件编程的学习!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Linux文件编程