linux touch 函数,C实现Linux之touch命令

Linux 的 touch 命令用来修改文件时间戳,或者新建一个不存在的文件,使用方式如下:

touch [-acm][-r ref_file(参照文件)|-t time(时间值)] file(文件名)

本实验基于C语言实现Linux touch命令的 [-acm]选项。

注:

touch file1.txt 更新 file1.txt 的存取和修改时间

touch -a file1.txt 改变 file1.txt 的读取时间记录

touch -c file1.txt 如果 file1.txt 不存在,不创建文件

touch -m file1.txt 改变 file1.txt 的修改时间记录

touch命令就是修改文件或者目录的时间属性,包括存取时间和更改时间。若文件不存在,系统会建立一个新的文件。

getopt函数:解决参数处理的函数。

//头文件

#include

//函数原型:

int getopt(int argc,char * const argv[ ],const char * optstring);

函数介绍:分析命令参数。

返回值:参数字符。

open函数:

函数 open 是 UNIX 系统的系统调用函数,区别于 C 语言库函数 fopen。

//头文件:

#include

//函数原型

int open(constchar*pathname, intflags);int open(constchar*pathname, int flags, mode_t mode);

函数作用:打开

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是基于Linux下的C语言实现的示例代码: 1. 引入相关头文件 ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> #include <linux/input.h> #include <sys/mman.h> #include <errno.h> #include <stdbool.h> #include <time.h> #include <SDL/SDL.h> #include <SDL/SDL_image.h> #include <SDL/SDL_ttf.h> ``` 需要安装SDL、SDL_image和SDL_ttf库。 2. 定义常量 ```c #define SCREEN_WIDTH 640 #define SCREEN_HEIGHT 480 #define IMAGE_WIDTH 320 #define IMAGE_HEIGHT 240 #define IMAGE_NUM 4 #define IMAGE_PATH "./image%d.bmp" ``` 定义屏幕宽度、高度、图片宽度、高度、图片数量和图片路径等常量。 3. 定义全局变量 ```c int touch_fd; int screen_fd; int *screen_mem; int current_image_index = 0; SDL_Surface *screen; SDL_Surface *images[IMAGE_NUM]; ``` 定义触摸屏文件描述符、屏幕文件描述符、屏幕内存映射指针、当前显示图片的索引和SDL界面和图片数组等全局变量。 4. 定义函数 ```c int init_touch_screen(); int init_screen(); int init_images(); void show_image(int index); void switch_image(int index); ``` 定义初始化触摸屏、初始化屏幕、初始化图片、显示图片和切换图片等函数。 5. 实现函数 ```c int init_touch_screen() { touch_fd = open("/dev/input/event0", O_RDONLY); if(touch_fd < 0) { printf("Open touch screen device failed.\n"); return -1; } return 0; } int init_screen() { screen_fd = open("/dev/fb0", O_RDWR); if(screen_fd < 0) { printf("Open screen device failed.\n"); return -1; } screen_mem = mmap(NULL, SCREEN_WIDTH * SCREEN_HEIGHT * 4, PROT_READ | PROT_WRITE, MAP_SHARED, screen_fd, 0); if(screen_mem == MAP_FAILED) { printf("Map screen memory failed.\n"); return -1; } screen = SDL_CreateRGBSurfaceFrom(screen_mem, SCREEN_WIDTH, SCREEN_HEIGHT, 32, SCREEN_WIDTH * 4, 0, 0, 0, 0); if(screen == NULL) { printf("Create screen surface failed.\n"); return -1; } SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0)); SDL_UpdateRect(screen, 0, 0, 0, 0); return 0; } int init_images() { int i; char image_path[64]; for(i = 0; i < IMAGE_NUM; i++) { sprintf(image_path, IMAGE_PATH, i + 1); images[i] = IMG_Load(image_path); if(images[i] == NULL) { printf("Load image %s failed: %s.\n", image_path, IMG_GetError()); return -1; } } return 0; } void show_image(int index) { SDL_Rect src_rect; SDL_Rect dest_rect; src_rect.x = 0; src_rect.y = 0; src_rect.w = IMAGE_WIDTH; src_rect.h = IMAGE_HEIGHT; dest_rect.x = (SCREEN_WIDTH - IMAGE_WIDTH) / 2; dest_rect.y = (SCREEN_HEIGHT - IMAGE_HEIGHT) / 2; dest_rect.w = IMAGE_WIDTH; dest_rect.h = IMAGE_HEIGHT; SDL_BlitSurface(images[index], &src_rect, screen, &dest_rect); SDL_UpdateRect(screen, 0, 0, 0, 0); } void switch_image(int index) { int i, j; SDL_Rect src_rect; SDL_Rect dest_rect; src_rect.x = 0; src_rect.y = 0; src_rect.w = IMAGE_WIDTH; src_rect.h = IMAGE_HEIGHT; dest_rect.x = (SCREEN_WIDTH - IMAGE_WIDTH) / 2; dest_rect.y = (SCREEN_HEIGHT - IMAGE_HEIGHT) / 2; dest_rect.w = IMAGE_WIDTH; dest_rect.h = IMAGE_HEIGHT; if(index == current_image_index) { return; } if(index > current_image_index) { for(i = current_image_index; i < index; i++) { SDL_BlitSurface(images[i], &src_rect, screen, &dest_rect); SDL_UpdateRect(screen, 0, 0, 0, 0); for(j = 1; j <= IMAGE_HEIGHT; j++) { SDL_BlitSurface(images[i + 1], &src_rect, screen, &dest_rect); SDL_UpdateRect(screen, 0, 0, 0, 0); SDL_Delay(2); dest_rect.y--; } } } else { for(i = current_image_index; i > index; i--) { SDL_BlitSurface(images[i], &src_rect, screen, &dest_rect); SDL_UpdateRect(screen, 0, 0, 0, 0); for(j = 1; j <= IMAGE_HEIGHT; j++) { SDL_BlitSurface(images[i - 1], &src_rect, screen, &dest_rect); SDL_UpdateRect(screen, 0, 0, 0, 0); SDL_Delay(2); dest_rect.y++; } } } current_image_index = index; } ``` 实现初始化触摸屏、初始化屏幕、初始化图片、显示图片和切换图片等函数。 6. 实现函数 ```c int main(int argc, char *argv[]) { int x, y; bool touch_down = false; int touch_down_x, touch_down_y; struct input_event touch_event; if(init_touch_screen() < 0) { return -1; } if(init_screen() < 0) { return -1; } if(init_images() < 0) { return -1; } show_image(current_image_index); while(true) { if(read(touch_fd, &touch_event, sizeof(touch_event)) == sizeof(touch_event)) { if(touch_event.type == EV_ABS) { if(touch_event.code == ABS_X) { x = (touch_event.value - 200) * SCREEN_WIDTH / 620; } else if(touch_event.code == ABS_Y) { y = (touch_event.value - 200) * SCREEN_HEIGHT / 380; } } else if(touch_event.type == EV_KEY) { if(touch_event.code == BTN_TOUCH) { touch_down = touch_event.value; if(touch_down) { touch_down_x = x; touch_down_y = y; } else { if(x < SCREEN_WIDTH / 2 && y < SCREEN_HEIGHT / 2) { switch_image(0); } else if(x < SCREEN_WIDTH / 2 && y >= SCREEN_HEIGHT / 2) { switch_image(1); } else if(x >= SCREEN_WIDTH / 2 && y < SCREEN_HEIGHT / 2) { switch_image(2); } else { switch_image(3); } } } } } } return 0; } ``` 实现函数,监听触摸屏的事件,根据触摸屏的位置切换图片,并实现图片切换效果。 7. 编译运行程序 使用以下命令编译程序: ``` gcc -o touch_screen touch_screen.c -lSDL -lSDL_image -lSDL_ttf ``` 使用以下命令运行程序: ``` sudo ./touch_screen ``` 8. 效果展示 当触摸屏位于左上区域时,显示image1.bmp图片并实现掉落效果;当触摸屏位于左下区域时,显示image2.bmp图片并实现滚动效果;当触摸屏位于右上区域时,显示image3.bmp图片并实现掉落效果;当触摸屏位于右下区域时,显示image4.bmp图片并实现滚动效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值