Linux 文件操作 open,lseek,read,write,close
2013-11-27 14:14
579 查看
int in ;
in = open("driver.log", O_RDWR|O_CREAT, S_IRUSR|S_IWUSR); /* 在当前目录以读写方式打开文件driver.log,没有则创建,
in为返回的文件“编号”,以后就用in代表driver.log,
in到driver.log涉及到的系统调用很复杂,我也没看过这里就不说了
S_IRUSR|S_IWUSR是用户权限,这个在创建文件的时候有用,最好加上
*/
lseek(in, 3, SEEK_SET); /* 把读写文件的位置定位在第4个字节的地方,读写是从0开始 */
write(in, Buff, 3); /* 从当前位置写入三个字节数据,Buff里面的值为 0x550x550x550x550x55 */
lseek(in, 0, SEEK_SET); /* 在将读写位置定位在0,也就是文件起始位置 */
number = read(in, buff, 10); /* 从当前位置读取10个字节,但是实际上只有6个,number返回文件里面字节个数 */
printf("%x\n", number); /* 打印number */
for(i=0; i<10; i++)
{
printf("%x ", buff[i]); /* 打印从文件中读到的数据,可以看到是00 00 00 55 55 55 ,确实定位在 3 是从第四个字节写入 */
}
close(in); /* 关闭文件in */
in = open("driver.log", O_RDWR|O_CREAT, S_IRUSR|S_IWUSR); /* 在当前目录以读写方式打开文件driver.log,没有则创建,
in为返回的文件“编号”,以后就用in代表driver.log,
in到driver.log涉及到的系统调用很复杂,我也没看过这里就不说了
S_IRUSR|S_IWUSR是用户权限,这个在创建文件的时候有用,最好加上
*/
lseek(in, 3, SEEK_SET); /* 把读写文件的位置定位在第4个字节的地方,读写是从0开始 */
write(in, Buff, 3); /* 从当前位置写入三个字节数据,Buff里面的值为 0x550x550x550x550x55 */
lseek(in, 0, SEEK_SET); /* 在将读写位置定位在0,也就是文件起始位置 */
number = read(in, buff, 10); /* 从当前位置读取10个字节,但是实际上只有6个,number返回文件里面字节个数 */
printf("%x\n", number); /* 打印number */
for(i=0; i<10; i++)
{
printf("%x ", buff[i]); /* 打印从文件中读到的数据,可以看到是00 00 00 55 55 55 ,确实定位在 3 是从第四个字节写入 */
}
close(in); /* 关闭文件in */
相关文章推荐
- Linux基础编程-文件操作open,lseek,read,write,close
- Linux基础编程-文件操作open,lseek,read,write,close
- linux文件操作open close read write lseek
- Linux基础编程-文件操作open,lseek,read,write,close
- linux文件操作函数(open、write、read、close)
- 【UNIX环境高级编程】文件 IO 操作 一 ( open | close | creat | lseek | write | read )
- linux文件I/O编程 (open、read、write、lseek、close)
- 1.Linux应用编程---文件I/O(open、read、write、lseek、close)
- Linux 文件IO函数(open/close/read/write/lseek)
- [转]Linux文件操作函数open close read write等示例
- linux 下文件I/O编程 (open、read、write、lseek、close)
- Linux-文件操作(open、write、read、close、iotcl)
- Linux C flie操作: open write read lseek close函数分析
- linux文件操作函数(open、write、read、close)可用于串口读写
- Linux文件IO-open,write,read,lseek,close
- Linux 文件系统调用函数open close read write lseek perror
- Linux文件I/O(creat/open/read/write/lseek/close/dup/sync)
- Linux系统开发 2 文件IO open() close() read() write() perror() lseek() fcntl() ioctl()
- Linux文件操作函数open close read write等示例
- 嵌入式linux文件I/O编程 (open、read、write、lseek、close)