Linux下文件操作
来源:互联网 发布:淘宝苹果数据线 编辑:程序博客网 时间:2024/05/17 00:18
代码如下:
代码很简单,刚开始一直读取不到数据,原来是在写完后,偏移地址指针处在文件末尾,则可用lseek函数使指针指向文件开头。
/*****
author:mlkk
date:2017.0.25
function:test the lseek
*****/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#define Size 20
int main(void)
{
int fd;
unsigned char i;
int ret;
unsigned char *W_buf = NULL;
unsigned char *R_buf = NULL ;
char *dev_mlkk = "/dev/test.txt";
fd = open(dev_mlkk, O_CREAT | O_RDWR | O_SYNC);
if (fd < 0)
{
printf(" open %s failure .\n",dev_mlkk);
return -1;
}
W_buf = (unsigned char *)malloc(Size * sizeof( unsigned char));
R_buf = (unsigned char *)malloc(Size * sizeof( unsigned char));
for (i=0; i < Size; i++)
W_buf[i] = i;
printf("open successfull .\n");
ret = write(fd, W_buf, Size );
printf("write %d .\n", ret);
lseek(fd, 0, SEEK_SET);
ret = read(fd, R_buf, Size);
if (ret > 0)
{
printf("read :%d :\n", ret);
for(i = 0; i < Size; i++)
{
printf("%d: %d \n",i,R_buf[i]);
}
}
else
{
printf("read failed .\n");
return -1;
}
free(W_buf);
free(R_buf);
W_buf = NULL;
R_buf = NULL;
close(fd);
}
- linux下文件操作
- Linux 下文件操作
- Linux下文件操作
- linux下文件操作
- linux下文件操作
- Linux 下文件操作
- Linux下文件操作
- Linux下文件操作
- Linux下文件操作
- Linux下文件的操作
- Linux下文件的操作
- Linux下文件的操作
- LINUX下文件操作函数
- linux下文件操作总结
- Linux下文件操作命令
- Linux 下文件的操作
- linux下文件操作函数
- Linux下文件操作命令
- Let the Balloon Rise
- 70. Climbing Stairs
- idTCPSever得到连接IP和端口。
- iOS开发之App主题切换完整解决方案(Swift版)
- 带权的完备二分匹配问题
- Linux下文件操作
- 大数据工程师面试题(六)
- 0002 真的吗,700元的电脑可以学会编程?
- git的操作命令
- Hibernate (一) 入门
- log4j,ConversionPattern的英文全拼说明
- 461. Hamming Distance
- 2017.2.25被虐记录
- zookeeper安装配置笔记