Linux环境编程--lseek ioctl
时间:2010-12-25 来源:willwwei
willisway1:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h> int main(int argc, char *argv[])
{
int fd;
int ret; if (argc < 2)
{
fprintf(stderr, "Usage: %s filename\n", argv[0]);
return -1;
} fd = open(argv[1], O_RDWR);
if (fd == -1)
{
perror("open");
return -1;
} ret = lseek(fd, 1 * 1024 * 1024 * 1024, SEEK_END);
printf("ret = %d\n", ret);
ret = write(fd, "", 1);
printf("ret = %d\n", ret); close(fd); return -1;
}
分析:移动文件读/写指针 willisway2: #include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h> #include <sys/ioctl.h>
#include <linux/cdrom.h> int main(void)
{
int fd, ret; fd = open("/dev/cdrom", O_RDONLY | O_NONBLOCK);
if (fd == -1)
{
perror("open");
return -1;
} ret = ioctl(fd, CDROMEJECT);
if (ret == -1)
{
perror("ioctl");
return -1;
} close(fd); return 0;
}
分析:控制I/O设备 willisway3: #include <unistd.h> #define FILENAME "./test" int main(void)
{
int fd;
int ret;
int flag; fd = open(FILENAME, O_WRONLY);
printf("fd = %d\n"); ret = write(fd, "1", 1);
if (ret == -1)
{
printf("ret = -1\n");
perror("write");
} flag = fcntl(fd, F_GETFL);
flag |= O_APPEND;
ret = fcntl(fd, F_SETFL, flag);
printf("fcntl ret = %d\n"); ret = write(fd, "2", 1);
printf("ret = %d\n", ret); flag = fcntl(fd, F_GETFL);
flag &= ~(O_APPEND);
fcntl(fd, F_SETFL, flag); return 0;
} 分析:控制I/O设备
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h> int main(int argc, char *argv[])
{
int fd;
int ret; if (argc < 2)
{
fprintf(stderr, "Usage: %s filename\n", argv[0]);
return -1;
} fd = open(argv[1], O_RDWR);
if (fd == -1)
{
perror("open");
return -1;
} ret = lseek(fd, 1 * 1024 * 1024 * 1024, SEEK_END);
printf("ret = %d\n", ret);
ret = write(fd, "", 1);
printf("ret = %d\n", ret); close(fd); return -1;
}
分析:移动文件读/写指针 willisway2: #include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h> #include <sys/ioctl.h>
#include <linux/cdrom.h> int main(void)
{
int fd, ret; fd = open("/dev/cdrom", O_RDONLY | O_NONBLOCK);
if (fd == -1)
{
perror("open");
return -1;
} ret = ioctl(fd, CDROMEJECT);
if (ret == -1)
{
perror("ioctl");
return -1;
} close(fd); return 0;
}
分析:控制I/O设备 willisway3: #include <unistd.h> #define FILENAME "./test" int main(void)
{
int fd;
int ret;
int flag; fd = open(FILENAME, O_WRONLY);
printf("fd = %d\n"); ret = write(fd, "1", 1);
if (ret == -1)
{
printf("ret = -1\n");
perror("write");
} flag = fcntl(fd, F_GETFL);
flag |= O_APPEND;
ret = fcntl(fd, F_SETFL, flag);
printf("fcntl ret = %d\n"); ret = write(fd, "2", 1);
printf("ret = %d\n", ret); flag = fcntl(fd, F_GETFL);
flag &= ~(O_APPEND);
fcntl(fd, F_SETFL, flag); return 0;
} 分析:控制I/O设备
相关阅读 更多 +