#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include<stdlib.h>
#include<error.h>
#include<linux/input.h>
void touch_mkdir(char *mkdir_name)
{
mkdir(mkdir_name, 0777);
}
void touch_file(char *mkdir_name, char *file_name)
{
//先进入对应的文件夹
chdir(mkdir_name);
//在对应的目录中创建文件
FILE *fp = fopen(file_name, "w+");
//关闭文件
fclose(fp);
//推出当前目录
chdir("..");
}
void insert_txt(char *mkdir_name, char *file_name)
{
char buf[300] = {0};
char file_path[300] = {0};
printf("输入你想写入的数据:\n");
scanf("%s", buf);
sprintf(file_path, "%s/%s", mkdir_name, file_name);
//打开对应的文件
FILE *fp = fopen(file_path, "w");
//将信息buf写入
fwrite(buf, strlen(buf), 1, fp);
fclose(fp);
}
void printf_file_inf(char *mkdir_name, char *file_name)
{
char seqs[] = ",";
char file_path[300] = {0};
sprintf(file_path, "%s/%s", mkdir_name, file_name);
char buf[300] = {0};
FILE *fp = fopen(file_path, "r");
memset(buf, 0, sizeof(buf));
int read_fd = fread(buf, 300, 1, fp);
char *temp = strtok(buf, seqs);
printf("%s\n", temp);
while (1)
{
temp = strtok(NULL, seqs);
if(temp!=NULL){
printf("%s\n", temp);
}else{
break;
}
}
fclose(fp);
}
int copy_file1_to_file2(char *mkdir_name, char *file_name1, char *file_name2)
{
char file_path_1[300] = {0};
char file_path_2[300] = {0};
sprintf(file_path_1, "%s/%s", mkdir_name, file_name1);
sprintf(file_path_2, "%s/%s", mkdir_name, file_name2);
char buf[20] = {0};
int ftell_1 = 0, ftell_2 = 0;
//打开文件file_name1,将数据写到buf[]中
FILE *fp_1 = fopen(file_path_1, "r");
FILE *fp_2 = fopen(file_path_2, "w");
//读取文件file_name1中字符串的长度
while (1)
{
memset(buf, 0, sizeof(buf));
ftell_1 = ftell(fp_1);
int read_fd = fread(buf, 4, 5, fp_1);
if (read_fd < 5)
{
if (ferror(fp_1))
{
printf("读取失败\n");
return -1;
}
else
{
ftell_2 = ftell(fp_1);
fwrite(buf, ftell_2 - ftell_1, 1, fp_2);
break;
}
}
else
{
fwrite(buf, 4, 5, fp_2);
}
}
fclose(fp_2);
fclose(fp_1);
}
int find_file(char *obj_path, char *obj_file)
{
//当前路径层开始检索
DIR *dp = opendir(obj_path);
if (dp == NULL)
{
perror("opendir");
return -1;
}
struct dirent *eq = NULL;
char TQ[260];
while (1)
{
memset(TQ, 0, 260);
eq = readdir(dp);
if (eq == NULL)
{
break;
}
//忽略. ..
if (eq->d_name[0] == '.')
{
continue;
}
//拼接名字路径
if (obj_path[strlen(obj_path) - 1] == '/')
{
sprintf(TQ,"%s%s", obj_path, eq->d_name);
}
else
{
sprintf(TQ,"%s/%s", obj_path, eq->d_name);
}
//这里就可以判断是文件还是目录了
if (eq->d_type == DT_DIR)
{
find_file(TQ,obj_file);
//这里的传参??
} //目录为DT_DIR(4)这里就可以递归了
if (eq->d_type == DT_REG)
{
if (strcmp(eq->d_name,obj_file) == 0)
{
printf("%s\n", TQ);
}
} //文件为DT_REG(8)
}
closedir(dp);
return 0;
}
int show_mkdir_file(char *mkdir_path){
char file_path[260];
//打开目录
DIR*dp= opendir(mkdir_path);
if (dp == NULL)
{
perror("opendir");
return -1;
}
//新建目录流指针
struct dirent*eq=NULL;
while (1)
{
memset(file_path,0,260);
eq=readdir(dp);
if (eq == NULL)
{
break;
}
if (eq->d_name[0] == '.')
{
continue;
}
if (mkdir_path[strlen(mkdir_path) - 1] == '/')
{
sprintf(file_path,"%s%s", mkdir_path, eq->d_name);
}
else
{
sprintf(file_path,"%s/%s", mkdir_path, eq->d_name);
}
//判断是文件还是目录
if (eq->d_type == DT_DIR)
{
show_mkdir_file(file_path);
}
if (eq->d_type == DT_REG)
{
printf("%s\n", file_path);
}
}
closedir(dp);
return 0;
}
int main(int argc, char *argv[])
{
//创建目录
touch_mkdir(argv[1]);
//在所创建的目录下创建文件夹
touch_file(argv[1], "klh.txt");
touch_file(argv[1], "hjy.txt");
//在hjy.txt中输入信息
insert_txt(argv[1], "hjy.txt");
//将hjy.txt中的内容复制到klh.txt;
copy_file1_to_file2(argv[1], "hjy.txt", "klh.txt");
//将文件中klh.txt的信息按照“,”分割出来并且打印出
printf_file_inf(argv[1], "klh.txt");
//寻找文件并且将文件路径打印
find_file("./", "klh.txt");
//将目录下的所有文件打印出来
show_mkdir_file("./");
}