69,369
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int read()
{
FILE *fp = fopen("./pipe","rb+");
int temp = fgetc(fp);
temp += 1;
rewind(fp);
fwrite(&temp, sizeof(int), 1, fp);
fclose(fp);
return temp;
}
void path(char * p, int nr)
{
*(p+0) = nr / 100 + '0';
*(p+1) = (nr / 10) % 10 + '0';
*(p+2) = nr % 10 + '0';
*(p+3) = '\0';
}
int copy(char * src)
{
FILE *fps = fopen(src, "rb");
if (!fps){
printf("%s doesn't exist.\n",src);
exit(-1);
}
char *p = (char*) malloc(10);
char *dest = (char*) malloc(100);
strcpy(dest,"./pic/");
path(p, read());
strcat(dest,p);
strcat(dest,".jpg\0");
FILE *fpd = fopen(dest,"wb");
if (!fpd) {
printf("Create failed.\n");
exit(-1);
}
while (!feof(fps)) fputc(fgetc(fps), fpd);
fclose(fps);
fclose(fpd);
free(p);
free(dest);
return 0;
}
void init()
{
FILE *fp = fopen("./pipe","r");
if (!fp){
//if there is no pipe then create one.
fp = fopen("./pipe","w");
}
fclose(fp);
}
void rm_space(char * src)
{
int i = 0;
while(*(src+i++));
i -= 2;
while (*(src+i) == '\040'){
*(src+i) = '\0';
i--;
}
}
int main(int argc, char *argv[20])
{
init();
char *src = (char *)malloc(100);
int i = 1;
*src = '\0';
while (i != argc){
if (i < argc){
strcat(src,argv[i++]);
strcat(src,"\040");
}
else
strcat(src,argv[i]);
}
rm_space(src);
copy(src);
free(src);
return 0;
}
#!/bin/bash
rm ./pipe
rm ./pic/*
gcc -o copy main.c
find '/home/etoux/Music/Nightwish APE/' -name '*.jpg' -exec './copy' {} \;
////////////////////////////////////////////////////////////////
//说明:
// 该程序配合find指令拷贝一个子目录下所有的图片到指定文件夹
////////////////////////////////////////////////////////////////
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//该函数从一个临时文件读出一个整数加一并返回临时文件。
//返回值:读书的整数
int read()
{
FILE *fp = fopen("./pipe","rb+");
int temp = fgetc(fp);
temp += 1;
rewind(fp);
fwrite(&temp, sizeof(int), 1, fp);
fclose(fp);
return temp;
}
//该函数将一个整数转化成字符串
//字符串存在p指针开始的内存。
void path(char * p, int nr)
{
*(p+0) = nr / 100 + '0';
*(p+1) = (nr / 10) % 10 + '0';
*(p+2) = nr % 10 + '0';
*(p+3) = '\0';
}
//该函数将src所指向的文件,复制到目标文件夹中。
int copy(char * src)
{
char *p = (char*) malloc(10);
char *dest = (char*) malloc(100);
//生成目标路径名。规范为./pic/nr.jpg
strcpy(dest,"./pic/");
path(p, read());
strcat(dest,p);
strcat(dest,".jpg");
FILE *fps = fopen(src, "rb");
if (!fps){
printf("%s doesn't exist.\n",src);
free(p);
free(dest);
return -1;
}
FILE *fpd = fopen(dest,"wb");
if (!fpd) {
printf("Create failed.\n");
free(p);
free(dest);
return -1;
}
//从源文件拷贝到了目标文件
while (!feof(fps)) fputc(fgetc(fps), fpd);
fclose(fps);
fclose(fpd);
free(p);
free(dest);
return 0;
}
//检查是否存在临时文件,如果没有就创建一个。
void init()
{
FILE *fp = fopen("./pipe","r");
if (!fp){
//if there is no pipe then create one.
fp = fopen("./pipe","w");
}
fclose(fp);
}
//删除掉一个字符串中尾端的空格
void rm_space(char * src)
{
int i = 0;
while(*(src+i++));
i -= 2;
//如果是空格,删除
while (*(src+i) == '\040'){
*(src+i) = '\0';
i--;
}
}
int main(int argc, char *argv[50])
{
init();
char *src = (char *)malloc(100);
int i = 1;
*src = '\0';
//将find中传入的多个参数(以空格隔开)合并成一个字符串
//即源文件的路径。
while (i != argc){
if (i < argc){
strcat(src,argv[i++]);
strcat(src,"\040");
}
else
strcat(src,argv[i]);
}
//删除掉字符串尾端的空格,我也不知道为什么会有空格在尾部。
//如果不删除将得不到正确结果
rm_space(src);
copy(src);
free(src);
return 0;
}
/*
#!/bin/bash
rm ./pipe
rm ./pic/*
gcc -o copy main.c
find '/home/etoux/Music/Nightwish APE/' -name '*.jpg' -exec './copy' {} \;
*/