#include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <stdbool.h> #include <sys/mman.h> #include <linux/input.h> #include <errno.h> #include <dirent.h> //设置节点 struct node { char *data; struct node *next; struct node *prev; }; //初始化链表 struct node *list_init() { struct node *head = malloc(sizeof(struct node));//malloc时动态内存分配函数 head->next = head; head->prev = head; return head; } //创建新节点 struct node *newnode(char *newdata) { struct node *new = malloc(sizeof(struct node)); new->data = newdata; new->next = NULL; new->prev = NULL; } //加入新节点 int addnode(struct node *new,struct node *list) { struct node *p = list; while(p->next != list) { p = p->next; } new->prev = list->prev; new->next = list; list->prev = new; new->prev->next = new; } int main(int argc,char **argv) { struct node *list = list_init();//初始化双向循环链表 DIR *dp = opendir(argv[1]); //打开目录文件argv[1] struct dirent *p; while(p = readdir(dp)) //遍历目录文件 { if(p->d_type == DT_REG) { if(strstr(p->d_name,".bmp")) //判断是否为.bmp文件 { struct node *new = newnode(p->d_name); //创建新节点 addnode(new,list); //插入新节点 } } } }
时间: 2024-03-07 17:52:03 浏览: 20
这段代码是一段 C 语言代码,主要实现的功能是遍历指定目录中的所有 BMP 格式的文件,并将它们的文件名存储在一个双向循环链表中。这个链表的每个节点包含一个字符串,即 BMP 文件的文件名。在 main 函数中,先调用 list_init 函数初始化一个双向循环链表,然后调用 opendir 函数打开指定的目录文件,再通过 readdir 函数遍历该目录下的所有文件和子目录。对于每个文件,判断它是否是 BMP 格式的文件,如果是,则调用 newnode 函数创建一个新的节点,并将文件名存储在该节点中,最后调用 addnode 函数将该节点插入到双向循环链表中。
相关问题
为程序添加备注注意对齐#include <stdio.h> #include <stdlib.h> #include <string.h> #include <dirent.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <pwd.h> #include <grp.h> #include <time.h> void shell_ls_l(char *file,struct stat st) { char limi
/*
* 该函数实现了类似于Linux终端中的ls -l命令的功能,可以显示文件的详细信息。
* 参数file为要显示信息的文件名;
* 参数st为文件的stat结构体,包含了文件的各种信息,如权限、大小、修改时间等。
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
void shell_ls_l(char *file,struct stat st) {
char limit[10]="rwxrwxrwx"; // 文件权限对应的字符
char perms[11]="----------"; // 文件权限字符串
int i=0;
int j=0;
int k=0;
struct passwd *user; // 用户信息结构体
struct group *group; // 用户组信息结构体
// 设置文件权限字符串
for(i=0;i<9;i++) {
if(st.st_mode & (1<<(8-i))) {
perms[i]=limit[i];
}
}
// 根据文件类型设置第一个字符
switch(st.st_mode & S_IFMT) {
case S_IFLNK:
perms[0]='l';
break;
case S_IFDIR:
perms[0]='d';
break;
case S_IFCHR:
perms[0]='c';
break;
case S_IFBLK:
perms[0]='b';
break;
case S_IFIFO:
perms[0]='p';
break;
case S_IFSOCK:
perms[0]='s';
break;
default:
perms[0]='-';
break;
}
printf("%s ",perms); // 输出文件权限
printf("%d ",(int)st.st_nlink); // 输出硬链接数
user=getpwuid(st.st_uid); // 获取文件拥有者信息
group=getgrgid(st.st_gid); // 获取文件拥有者组信息
printf("%s %s ",user->pw_name,group->gr_name); // 输出拥有者和所属组
printf("%8ld ",st.st_size); // 输出文件大小
char *time_str=ctime(&st.st_mtime); // 获取文件最后修改时间
time_str[strlen(time_str)-1]='\0'; // 去掉不需要的换行符
printf("%s ",time_str); // 输出最后修改时间
printf("%s\n",file); // 输出文件名
}
/* Socket head file */ #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h>/*sockaddr_in{}*/ #include<arpa/inet.h> /* Unix standard head file */ #include<stdio.h> #include<stdlib.h> #include<string.h> #include<unistd.h> #include <errno.h>/* strerror(),perror(),errno head file*/ #include <sys/time.h> #include <sys/wait.h> #define MAXBUFF 1024 #define PORT 9999怎么改成windows系统
在Windows系统中,网络编程需要使用Winsock API,因此需要引入`<winsock2.h>`头文件。此外,需要在程序中调用`WSAStartup()`函数和`WSACleanup()`函数来初始化和释放Winsock库。
下面是在Windows系统下进行网络编程的头文件和常量定义示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "Ws2_32.lib")
#define MAXBUFF 1024
#define PORT 9999
```
其中,`<ws2tcpip.h>`头文件包含了一些网络编程相关的函数和结构体定义。`#pragma comment(lib, "Ws2_32.lib")`语句用于链接Winsock库。
需要注意的是,在Windows系统中,端口号的类型为`unsigned short`,而不是`int`。因此,定义端口号常量时应该使用`USHORT`类型:
```c
#define PORT (USHORT)9999
```