#include
#include
off_t lseek(int fd, off_t offset, int whence);
off_t currpos;
currpos = lseek(fd, 0, SEEK_CUR);
#include
#include
#include
#include
#include
#include
int main(void)
{
int fd = open("aaa", O_RDWR);
if (fd < 0)
{
perror("aaa");
exit(1);
}
//拓展一个文件,一定要有一次写的操作
lseek(fd, 0x1000, SEEK_SET);
write(fd, "c", 1);
close(fd);
fd = open("hello", O_RDWR);
if (fd < 0)
{
perror("hello");
exit(1);
}
//求文件的大小
printf("hello size:%d\n", lseek(fd, 0, SEEK_END));
close(fd);
return 0;
}
#include
#include
int fcntl(int fd, int cmd);
int fcntl(int fd, int cmd, long arg);
int fcntl(int fd, int cmd, struct flock *lock);
#include
#include
#include
#include
#include
#include
#include
#include
#define MSG_AGN "try again\n"
int main(void)
{
char buf[10] = {0};
int n = 0;
int flags = 0;
flags = fcntl(STDIN_FILENO, F_GETFL);
flags |= O_NONBLOCK;
if (fcntl(STDIN_FILENO, F_SETFL, flags) == -1)
{
perror("fcntl");
exit(1);
}
tryagain:
n = read(STDIN_FILENO, buf, 10);
if (n < 0)
{
if (errno == EAGAIN)
{
sleep(5);
write(STDOUT_FILENO, MSG_AGN, strlen(MSG_AGN));
goto tryagain;
}
printf("errno num: %d\n", errno);
perror("read stdin");
exit(1);
}
write(STDOUT_FILENO, buf, n);
printf("\n");
close(flags);
return 0;
}
#include
int ioctl(int d, int request, ...);
d是某个设备的文件描述符。request是ioctl的命令,可变参数取决于request,通常是
一个指向变量或结构体的指针。若出错则返回-1,若成功则返回其他值,返回值也是取决于request。
#include
#include
#include
#include
int main(void)
{
struct winsize size;
if (isatty(STDOUT_FILENO) == 0)
{
exit(1);
}
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0)
{
perror("ioctl TIOCGWINSZ error");
exit(1);
}
printf("%d rows, %d columns\n", size.ws_row, size.ws_col);
return 0;
}