Linux项目一
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项目一的更多相关文章
- Atitit. 查找linux 项目源码位置
Atitit. 查找linux 项目源码位置 1. netstat -anp |grep 801 1.1. 1.3 启动关闭nginx3 1.2. 找到nginx配置文件4 1.3. ./etc/ ...
- 部署Linux项目
部署Linux项目 1● 下载软件 ftp 安装 2● 创建连接 3● java项目 gunzip –c *.gz tar –xzf *.gz rm –rf rm -r ...
- VisualGDB:使用VS创建CMake Linux项目
转载地址:点击打开链接 根据VisualGDB官网(https://visualgdb.com)的帮助文档大致翻译而成.主要是作为个人学习记录.有错误的地方,Robin欢迎大家指正. 本文介绍如何使用 ...
- VisualGDB系列8:使用VS创建CMake Linux项目
根据VisualGDB官网(https://visualgdb.com)的帮助文档大致翻译而成.主要是作为个人学习记录.有错误的地方,Robin欢迎大家指正. 本文介绍如何使用VS来创建.构建.调试一 ...
- Linux项目部署发布
Linux项目部署发布 1.部署环境准备,准备python3和虚拟环境解释器,virtualenvwrapper pip3 install -i https://pypi.douban.com/sim ...
- VisualGDB系列9:配置VS直接通过SSH方式访问Linux项目
根据VisualGDB官网(https://visualgdb.com)的帮助文档大致翻译而成.主要是作为个人学习记录.有错误的地方,Robin欢迎大家指正. 本文介绍如何使用VS和VisualGDB ...
- VisualGDB系列6:远程导入Linux项目到VS中
根据VisualGDB官网(https://visualgdb.com)的帮助文档大致翻译而成.主要是作为个人学习记录.有错误的地方,Robin欢迎大家指正. 本文介绍如何将Linux机器上的Linu ...
- Linux项目发布流程
Linux项目发布流程(一) 1.安装pyhton3.7 的依赖包 yum -y groupinstall "Development tools" yum -y install z ...
- linux 项目环境搭建配置
经过三天加一上午的努力折腾,本地项目终于跑起来了,linux系统,重装后需要安装基本的php,nginx,mysql.php扩展需要安装curl ,memcache,memcached等.然后就是修改 ...
随机推荐
- 国都企信通短信平台发送手机短信的python脚本一例
一年前,由于工作需要,给以色列的同事解释一下国都短信平台的短信发送格式,本来不懂python的我硬着头皮写了一个sample,比较粗,能用,但不优美,希望以后学会python能改得像我同事写的那么优雅 ...
- checkbox 全选/取消
<html><script language=javascript>function selectAll(){var a = document.getElementsByTag ...
- android EditText设置光标、边框和图标
控制边框形状,先在drawable中建一个xml文件:shape.xml <?xml version="1.0" encoding="utf-8"?> ...
- js异步的理解---千呼万唤始出来啊!
编译完成后(先分配给变量空间和function(){}命名的函数,var = function(){}这种函数也仅仅只是分配了个空间,还没有赋值个函数给他!),调用了若不是undefined就执行, ...
- cancel-ng-swipe-right-on-child
<!DOCTYPE html> <html lang="en" ng-app="myapp"> <head> <met ...
- 【转】ibatis的简介与初步搭建应用
[转]ibatis的简介与初步搭建应用 一.ibatis的简介 ibatis是什么东西就不介绍了,自己去找谷老师. 这里讲下自己的使用体会.之前自己学过Hibernate,是看尚学堂的视频教学的,看完 ...
- Ext4 简单的treepanel
转载:http://blog.csdn.net/zyujie/article/details/8208499 最近在学习Ext4,记录一些有关Ext4实现控件的方法: Ext4的treePanel和之 ...
- 【USACO 3.2.6】香甜的黄油
[描述] 农夫John发现做出全威斯康辛州最甜的黄油的方法:糖.把糖放在一片牧场上,他知道N(1<=N<=500)只奶牛会过来舔它,这样就能做出能卖好价钱的超甜黄油.当然,他将付出额外的费 ...
- Linux导航神器-----autojump
对于命令行用户来说,频繁的cd和tab应该是日常工作中最多使用的命令了.特别对于重度用户来说,如果可以省去这么多cd和tab,将更多的时间做有意义的事该多好.其实Linux的学习过程本身就行这样.你会 ...
- underscorejs-some学习
2.11 some 2.11.1 语法: _.some(list, predicate, [context]) 2.11.2 说明: 对list集合的每个成员根据predicate进行真值检测,如果一 ...