现在的位置: 首页 > 综合 > 正文

open close read write lseek函数的用法

2013年10月13日 ⁄ 综合 ⁄ 共 569字 ⁄ 字号 评论关闭

1:open 函数

头文件 #include<fcntl.h>

int open(const cahr *pathname  /*被打开的文件*/

                        const char flags, 

);

 

 

/*
* test.c
*
* Created on: Jun 26, 2012
* Author: linux
*/
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<fcntl.h>
int main() {
char buf_write[] = "abcedfg";
char buf_read[10];
int fd;
if ((fd = open("/test/hello.txt", O_RDWR, 0666)) == 0) {
printf("error");
close(fd);
}
int len = strlen(buf_write)+1;
//int len=5;
int size = write(fd, buf_write, len);
printf("%d\n", size);
//puts(buf_write);
lseek(fd, 0, SEEK_SET);
size = read(fd, buf_read, len);
puts(buf_read);
close(fd);
return 0;
}

抱歉!评论已关闭.