Linux项目一

引言:

  这是我去年做的东西,一直没有时间整理,今天又要做一个基于这个项目的客户端与服务器版本。

  以前我写的库文件中的函数耦合度很大,在一个函数中调用另一个函数,导致一无法拆开使用!

  因此,我对以前写的库进行接口的更改,以更好的适应更多的项目。

  当然我也把以前写的小程序贴出来,以供大家参考!

需求:

  1.搜索bmp图片转换成黑白图片复制到另一个文件中

  2.系统为Linux

自定义功能函数:

  1.打印目录:

 #include<unistd.h>
#include<stdio.h>
#include<dirent.h>
#include<string.h>
#include<sys/stat.h>
#include<stdlib.h>
void printdir(char *dir,int depth)/*打印目录*/
{
DIR *dp;
struct dirent *entry;
struct stat statbuf; if((dp=opendir(dir))==NULL)
{
fprintf(stderr,"cannot open directory:%s\n",dir);
return;
}
chdir(dir);
while((entry=readdir(dp))!=NULL)
{
lstat(entry->d_name,&statbuf);/**获取文件信息,保存在statbuf**/
if(S_ISDIR(statbuf.st_mode))/***判断是否为目录***/
{
if(strcmp(".",entry->d_name)==||strcmp("..",entry->d_name)==)
continue;
printf("%*s%s/\n",depth," ",entry->d_name);
printdir(entry->d_name,depth+);
}
else
printf("%*s%s/\n",depth," ",entry->d_name);/***为文件***/
}
chdir("..");
closedir(dp);
}

  2.删除文件:

 #include<unistd.h>
void del_file(const char *filename)
{
unlink(filename);
return ;
}

  3.复制文件:

#include<unistd.h>
void lkbmp(const char *source,const char *target)
{
if(link(source,target)==)
printf(" link is successful\n");
else
printf(" link is error\n");
}

  4.对彩色图片转换成黑白图片

#include<stdio.h>
#include<string.h>
int bmp(char filename[])
{
FILE *fo,*fin;
RGN rgb,black={,,,},white={,,,};
char name[]="\0";
unsigned char temp[]="\0";
unsigned int offset=;
/***在文件名中添加copy_来区别原文件***/
int len=strlen(filename);
strncpy(name,filename,len-);
strcat(name,"_copy.bmp"); if((fo=fopen(filename,"rb+"))==NULL)
printf("can not open %s",filename);
if((fin=fopen(name,"wb+"))==NULL)
printf("can not open %s",name);
fseek(fo,,);
fread(&offset,,,fo);
printf("%X\n",offset);
fseek(fo,,);
unsigned int i=;
while(i<=offset)
{
fread(temp,,,fo);
fwrite(temp,,,fin);
i+=;
}
while(!feof(fo))
{
fread(&rgb,,,fo);
if((rgb.Blue+rgb.Green+rgb.Red)/<)
fwrite(&black,,,fin);
else
fwrite(&white,,,fin);
} fclose(fo);
fclose(fin);
return ;
}

完整自定义库文件:

 #include<unistd.h>
#include<stdio.h>
#include<dirent.h>
#include<string.h>
#include<stdlib.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<stdlib.h>
#include<time.h>
#include <sys/msg.h>
FILE *fp,*fbmp,*flog;
struct dirent *entry;
struct stat statbuf;
unsigned short BMP=0x4D42,
JPG=0xD8FF,
PNG[]={0x5089,0x474E,0x0A0D,0x0A1A},
GIF[]={0x4947,0x3846,0x6139};
static char path1[],path2[],watchdog[],name[];
struct tm *tm_ptr;
time_t the_time; //判断图片头文件的结构体
typedef struct tagRGBQUAD
{
unsigned char Blue;// À¶É«µÄÁÁ¶È£šÖµ·¶Î§Îª0-255)
unsigned char Green; // ÂÌÉ«µÄÁÁ¶È£šÖµ·¶Î§Îª0-255)
unsigned char Red; // ºìÉ«µÄÁÁ¶È£šÖµ·¶Î§Îª0-255)
unsigned char Reserved;// ±£Áô£¬±ØÐëΪ0
} RGN; void init()/*用于初始化程序*/
{
printf("file opened now\n");
fp=fopen("wrg.text","w+");
flog=fopen("wrlog.text","w+");
fbmp=fopen("bmpname.text","w+");
}
void Exit()
{
printf("everything is gone\n");
fclose(fp);
fclose(fbmp);
fclose(flog);
}
int istupian(char entry[])
/*返加值为
1 bmp;
2 jpg;
3 png;
4 gif;
*/
{
FILE *fis;
short int i=;
unsigned short pis[];
int flag=;//每次调用都进行初始化
if((fis=fopen(entry,"r"))==NULL)
printf("can not read %s\n",entry);
fread(pis,,,fis); if(pis[]==BMP)
{
flag=;
printf("it is a bmp\n");
}
else if(pis[]==JPG)
{
flag=;
printf("it is a jpg\n");
}
else if(PNG[]==pis[]&&PNG[]==pis[]&&PNG[]==pis[]&&PNG[]==pis[])
{
flag=;
printf("it is a png\n");
}
else if(GIF[]==pis[]&&GIF[]==pis[]&&GIF[]==pis[])
{
flag=;
printf("it is a gif\n");
}
return flag; }
void wrtlog()/*将文件的路径写入路径中*/
{
printf("write to wrtlg\n");
fprintf(fp,"%s/%s\n",getcwd(NULL,),entry->d_name);
fprintf(fbmp,"%s\n",entry->d_name);
}
void wrlog(char name[])// 将日记写入文件中
{
static int i=;
(void)time(&the_time);
//tm_ptr=gmtime(&the_time);
if(i==) fprintf(flog,"文件名 /状态 /时间 /\n");
++i;
fprintf(flog,"%s\t\t删除\t\t ",name); fprintf(flog,"%s\n",ctime(&the_time));
//fprintf(flog,"%02d:%02d:%02d:%02d:%02d:%02d\n",tm_ptr->tm_year,tm_ptr->tm_mon+1,tm_ptr->tm_mday,tm_ptr->tm_hour,tm_ptr->tm_min,tm_ptr->tm_sec);
}
void printdir(char dir[])/*打印目录*/
{
DIR *dp;
printf("printdir ******doing now\n");
if((dp=opendir(dir))==NULL)
{
fprintf(stderr,"cannot open directory:%s\n",dir);
return;
}
chdir(dir);
while((entry=readdir(dp))!=NULL)
{
lstat(entry->d_name,&statbuf);
if(S_ISDIR(statbuf.st_mode))
{
if(strcmp(".",entry->d_name)==||strcmp("..",entry->d_name)==)
continue;
printdir(entry->d_name);
}
else
wrtlog();
}
chdir("..");
closedir(dp);
}
int bmp(char filename[])
{
FILE *fo,*fin;
RGN rgb,black={,,,},white={,,,};
char name[]="\0";
unsigned char temp[]="\0";
unsigned int offset=;
/***在文件名中添加copy_来区别原文件***/
int len=strlen(filename);
strncpy(name,filename,len-);
strcat(name,"_copy.bmp"); if((fo=fopen(filename,"rb+"))==NULL)
printf("can not open %s",filename);
if((fin=fopen(name,"wb+"))==NULL)
printf("can not open %s",name);
fseek(fo,,);
fread(&offset,,,fo);
printf("%X\n",offset);
fseek(fo,,);
unsigned int i=;
while(i<=offset)
{
fread(temp,,,fo);
fwrite(temp,,,fin);
i+=;
}
while(!feof(fo))
{
fread(&rgb,,,fo);
if((rgb.Blue+rgb.Green+rgb.Red)/<)
fwrite(&black,,,fin);
else
fwrite(&white,,,fin);
} fclose(fo);
fclose(fin);
return ;
}
//硬连接
/*返加值为
1 bmp;
2 jpg;
3 png;
4 gif;
*/
void lkbmp()
{
fseek(fp,,);
fseek(fbmp,,);
while(fscanf(fp,"%s",path1)!=EOF&&fscanf(fbmp,"%s",name)!=EOF)
{
/***path2 的路径已经初始化,将文件名接到path2中,链接好后,将watchsog重新载入watchdog中***/
strcat(path2,name);
printf("%s",path2);
if(link(path1,path2)==)
{
printf(" link is successful\n");
}
else
printf(" link is error\n");
//恢复母路径
if(istupian(path2)==)
bmp(path2);
strcpy(path2,watchdog);
} }
//删除非图片文件
int del_dir()
{
fseek(fp,,);
fseek(fbmp,,);
while(fscanf(fbmp,"%s",name)!=EOF)
{
//path2 的路径已经初始化,将文件名接到path2中,链接好后,将watchsog重新载入watchdog中
strcat(path2,name);
if(!istupian(path2)&&unlink(path2)==)
{unlink(path2);
printf("unlink %s is successful\n",path2);
wrlog(path2);
}
else
printf("unlink %s is error\n",path2);
//恢复母路径
strcpy(path2,watchdog);
}
return ; }

对应的执行文件:

 #include"myhead.h"
#pragma pack(2)
int main(int argc,char *argv[])
{
char topdir[] = ".";
if(argc>=)
strcpy(topdir,argv[]);
printf("已经得到目录:\"%s\"\n",topdir);
printf("初始化中");
init();
printf("初始化完毕\n");
printf("start printdir\n");
printf("进入原始路径,得到母路径。");
chdir(topdir);
chdir("..");
strcpy(path2,getcwd(NULL,));
//创建D目录,可以以作为子进程
strcat(path2,"/D/");
mkdir(path2,);
printf("母路径:%s\n",path2);
strcpy(watchdog,path2);
pid_t fork_res;
fork_res=fork();
if(fork_res==)
{
printf("Child now\n");
while(del_dir()!=);
printf("del_dir done.\n");
return ;
}
else if(fork_res>)
{
printdir(topdir);
printf("printdir done.\n");
lkbmp();
printf("lkbmp done\n");
del_dir();
return ;
}
else
{ perror("fork failed");
exit();
} printf("程序运行结束");
exit();
}

  

Linux项目一的更多相关文章

  1. Atitit. 查找linux 项目源码位置

    Atitit. 查找linux 项目源码位置 1. netstat   -anp |grep 801 1.1. 1.3 启动关闭nginx3 1.2. 找到nginx配置文件4 1.3. ./etc/ ...

  2. 部署Linux项目

    部署Linux项目   1● 下载软件 ftp       安装 2● 创建连接 3● java项目   gunzip –c *.gz tar –xzf *.gz       rm –rf rm -r ...

  3. VisualGDB:使用VS创建CMake Linux项目

    转载地址:点击打开链接 根据VisualGDB官网(https://visualgdb.com)的帮助文档大致翻译而成.主要是作为个人学习记录.有错误的地方,Robin欢迎大家指正. 本文介绍如何使用 ...

  4. VisualGDB系列8:使用VS创建CMake Linux项目

    根据VisualGDB官网(https://visualgdb.com)的帮助文档大致翻译而成.主要是作为个人学习记录.有错误的地方,Robin欢迎大家指正. 本文介绍如何使用VS来创建.构建.调试一 ...

  5. Linux项目部署发布

    Linux项目部署发布 1.部署环境准备,准备python3和虚拟环境解释器,virtualenvwrapper pip3 install -i https://pypi.douban.com/sim ...

  6. VisualGDB系列9:配置VS直接通过SSH方式访问Linux项目

    根据VisualGDB官网(https://visualgdb.com)的帮助文档大致翻译而成.主要是作为个人学习记录.有错误的地方,Robin欢迎大家指正. 本文介绍如何使用VS和VisualGDB ...

  7. VisualGDB系列6:远程导入Linux项目到VS中

    根据VisualGDB官网(https://visualgdb.com)的帮助文档大致翻译而成.主要是作为个人学习记录.有错误的地方,Robin欢迎大家指正. 本文介绍如何将Linux机器上的Linu ...

  8. Linux项目发布流程

    Linux项目发布流程(一) 1.安装pyhton3.7 的依赖包 yum -y groupinstall "Development tools" yum -y install z ...

  9. linux 项目环境搭建配置

    经过三天加一上午的努力折腾,本地项目终于跑起来了,linux系统,重装后需要安装基本的php,nginx,mysql.php扩展需要安装curl ,memcache,memcached等.然后就是修改 ...

随机推荐

  1. codevs 1994 排队 排列组合+高精度

    /* 数学题0.0 最后答案:A(n,n)*A(n+1,2)*A(n+3,m)+A(n,n)*C(m,1)*A(2,2)*C(n+1,1)*A(n+2,m-1); 简单解释一下 +之前的很显然 先排男 ...

  2. HTML5 Canvas Text实例1

    1.简单实例1 <canvas width="300" height="300" id="canvasOne" class=" ...

  3. (转)mysql 的 find_in_set函数使用方法

    举个例子来说: 有个文章表里面有个type字段,他存储的是文章类型,有 1头条,2推荐,3热点,4图文 .....11,12,13等等 现在有篇文章他既是 头条,又是热点,还是图文, type中以 1 ...

  4. HexColorPicker 让选色变得更简单[for Mac]

    开发iOS的筒子看过来,走过路过,一不小心就错过~ Xcode里的颜色选择器,不能让你随意制定十六进制的颜色,让选色变成了一种折磨,然而作为开发者和设计师又得经常要用到. 现在有了HexColorPi ...

  5. jvectormap 中国地图 (包括香港、台湾、澳门)

    一个完整的中国地图(各个省,市.还有国两制),谢谢大家. 忘了网上哪位的范例,我加了些修改. <html xmlns="http://www.w3.org/1999/xhtml&quo ...

  6. javascript Array类型 方法大全

    1,创建数组 //第一种是使用Array构造函数 var colors = new Array(); var colors = new Array(20); //创建length为20的数组 var ...

  7. jdbc 连接mysql Communications link failure的解决办法

    使用Connector/J连接MySQL数据库,程序运行较长时间后就会报以下错误: Communications link failure,The last packet successfully r ...

  8. MySql存储过程—3、变量

    1.变量的定义 在Mysql里面可以像我们写代码中一样定义变量来保持中间结果,看下面的格式: DECLARE variable_name datatype(size) DEFAULT default_ ...

  9. 如何编写pythonGNURADIO应用

    三个基本概念: flow graph 很大一部分GNURADIO程序就是一个流图.流图包括若干信号处理模块(BLock),以及一些用表示数据流的边. BLock 所有信号处理过程在block中进行.在 ...

  10. typedef , static和 extern

    typedef 1.作用:给已经存在的类型起一个新的名称 2.使用场合: 1> 基本数据类型 2> 指针 3> 结构体 4> 枚举 5> 指向函数的指针 #include ...