博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
linux下文件操作的小demo
阅读量:3781 次
发布时间:2019-05-22

本文共 1154 字,大约阅读时间需要 3 分钟。

 linux下常见的操作文件的函数,open,write,lseek (本文未使用到),read,close

#include 
//包含write,,write等#include
#include
#include
//包含open函数#include
//标准输入输出,包含printfint main(){ int fd,size; //fd为文件描述符,size为读到的数据大小 char s[]="linux programmer!\n ",buffer[80]; fd=open("/tmp/temp",O_WRONLY|O_CREAT|O_APPEND); //打开文件,如没有就创建文件 write(fd,s,sizeof(s)); //写字符串到文件中 close(fd); //关闭文件 fd=open("/tmp/temp",O_RDONLY); //打开只读文件 size=read(fd,buffer,sizeof(buffer)); //将文件内容读入buffer字符串数组中 close(fd); printf("the size of is %d\n",size); printf("%s",buffer); //输出遇到\0结束}

注意:在llinux下,文件操作也有关于权限问题,所以在执行的时候最好加上sudo

//在命令行执行以下操作gcc file_test.c -o file_test  //编译该文件sudo ./file_test    //执行该文件
//输出如下the size of is 80linux programmer!

查看被写的文件

cat /tmp/temp

输出如下:(需多执行几下就可以看到同样效果)

linux programmer! linux programmer! linux programmer! linux programmer! linux programmer! linux programmer! linux programmer! linux programmer!

 

转载地址:http://qumvn.baihongyu.com/

你可能感兴趣的文章
行为型模式:模板方法
查看>>
spring cloud之Feign的使用
查看>>
Codeforces Round #617 (Div. 3) String Coloring(E1.E2)
查看>>
LeetCode刷题 --杂篇 --数组,链表,栈,队列
查看>>
840. 模拟哈希表(模板)
查看>>
《算法》笔记 17 - 数据压缩
查看>>
Qt Installer Framework翻译(5-2)
查看>>
Java+Selenium+Testng自动化测试学习(三)— 断言
查看>>
PAT乙级1012
查看>>
银行业务队列简单模拟(队列queue)
查看>>
MySql中的数据查询语言(DQL)三:连接查询
查看>>
MySql中的数据查询语言(DQL)五:union和limit
查看>>
数据操作语言(DML)一:插入数据insert、修改数据update、删除delete
查看>>
.properties 文件,.yml 文件 ,yaml文件语法学习
查看>>
jsp 的常用标签
查看>>
Listener 监听器
查看>>
SpringBoot自动配置原理
查看>>
IDEA连接mysql又报错设置时区!Server returns invalid timezone.
查看>>
员工管理系统二:首页和国际化实现
查看>>
员工管理系统四:员工列表实现
查看>>