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. COGS 859. 数列

    /* 先来说一下第一眼看到想出的奇葩方法23333.. 找每个数左右有几个比他小的 前几天刚学了区间第k小的求法 然后... 枚举中间的那个点 对于左区间 二分找到他是第几大 右区间同理 然后 *起来 ...

  2. 使用<input>标签做了两个按钮, 按钮之间间距如何去掉

    遇到的问题: 使用<input>标签做了两个按钮, 按钮之间有个间距不知道怎么去掉. 如下图: 问题解决: <input>是内联块状元素(inline-block); 内联元素 ...

  3. MSChart使用之动态生成多个多行ChartArea

    前台代码: <asp:Chart ID=" > <Titles> <asp:Title Name="Title1" runat=" ...

  4. java 懒汉式--初步解决安全问题

    2016-07-28 00:10:14 懒汉式: class text { public String k;       private static text t=null;//右边代码结构比上边饿 ...

  5. 【Usaco2008 Mar】土地购买

    [题目描述] 农夫John准备扩大他的农场,他正在考虑N (1 <= N <= 50,000) 块长方形的土地. 每块土地的长宽满足(1 <= 宽 <= 1,000,000; ...

  6. CentOS 6.5下安装MySql 5.7

    不管您按下面的方法安装成功否,请留个言,把您遇到的问题写上共勉! 包下载http://url.cn/WrNg5S 环境: 1).软硬件:E6420双核CPU,8G内存,1T硬盘 2).虚拟机下 Cen ...

  7. php常用的排序算法与二分法查找

    一 : 归并排序 将两个的有序数列合并成一个有序数列,我们称之为"归并".归并排序(Merge Sort)就是利用归并思想对数列进行排序.根据具体的实现,归并排序包括"从 ...

  8. CSS样式margin:0 auto不居中

    <style type="text/css">html,body{height:100%;width:960px;}.container{background-colo ...

  9. java 面试

        115个Java面试题和答案——终极列表(上) 本文我们将要讨论Java面试中的各种不同类型的面试题,它们可以让雇主测试应聘者的Java和通用的面向对象编程的能力.下面的章节分为上下两篇,第一 ...

  10. 绑定dropdownlist

    System.Data.SqlClient.SqlConnection sqlconn = new System.Data.SqlClient.SqlConnection(); sqlconn.C; ...