您的位置:首页 > 编程语言 > PHP开发

检查文件是否存在,将结果通过ftp上传

2011-04-08 14:56 381 查看
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>

#define UP_FTP_NAME "up_ftp.txt"

#define COUNT 2

int n=0;

void handle_file(char *file) {
char c;
int ret;

FILE *s_fp1,*d_fp2;

char hostname[128];
char writefile[128];
char nowtime[128];

struct tm *p1;
time_t timep;

timep=time(NULL);
p1=localtime(&timep);

// cygwin env
char cygwin[128];
sprintf(cygwin,"set CYGWIN=nodosfilewarning");
system(cygwin);

gethostname(hostname,sizeof(hostname));
// printf("%s/n",hostname);
sprintf(nowtime,"%d-%02d-%02d-%02d-%02d/n",1900+p1->tm_year,(1+p1->tm_mon),p1->tm_mday,p1->tm_hour,p1->tm_min,p1->tm_sec);
sprintf(writefile,"%s_%d-%02d-%02d-%02d-%02d.txt",hostname,1900+p1->tm_year,(1+p1->tm_mon),p1->tm_mday,p1->tm_hour,p1->tm_min,p1->tm_sec);
// printf("%s/n",writefile);
d_fp2=fopen(writefile,"a+");
if(d_fp2==NULL) {
fprintf(stderr,"fopen %s error./n",writefile);
exit(-1);
}

if(n++==0) {
fprintf(d_fp2,"%s/n",nowtime);
fprintf(d_fp2,"%s/n/n",hostname);
}

s_fp1=fopen(file,"r");
if(s_fp1==NULL) {
fprintf(stderr,"warning!! %s not exist./n",file);
fprintf(d_fp2,"warning!! %s not exist./n",file);
}
else {
fprintf(stderr,"ok!! %s exitst./n",file);
fprintf(d_fp2,"ok!! %s exitst./n",file);

ret=fseek(s_fp1,0,SEEK_SET);
if(ret==-1) {
fprintf(stderr,"fseek error./n");
}

if(fscanf(s_fp1,"%c",&c)==EOF) {
fprintf(stderr,"warning!! %s size is 0./n",file);
fprintf(d_fp2,"warning!! %s size is 0./nn",file);
}
else {
fprintf(stderr,"ok!! %s size is not 0./n",file);
fprintf(d_fp2,"ok!! %s size is not 0./n",file);
}
}

ret=fclose(s_fp1);
if(ret==EOF) {
fprintf(stderr,"fclose %s error./n",file);
exit(-1);
}

ret=fclose(d_fp2);
if(ret==EOF) {
fprintf(stderr,"fclose %s error./n",file);
exit(-1);
}
}

ftp_up_cmd() {
//----------ftp define----------------------
FILE *ftp_fp;
int i=0,ret;

char *up_ftp_parm[9]={"open 127.0.0.1","xunjian","123456","cd shipinshangchuan","bin","put GATEWAY1*","!rm GATEWAY1*","close","bye"};

char ftp_cmd[128];
//------------------------------------------

//----------------------------------------------------------------------
ftp_fp=fopen(UP_FTP_NAME,"a");
if(ftp_fp==NULL) {
fprintf(stderr,"fopen %s error./n",UP_FTP_NAME);
exit(-1);
}

for(i=0;i<9;i++) {
ret=fprintf(ftp_fp,"%s/n",up_ftp_parm[i]);
if(ret<0) {
fprintf(stderr,"fprintf error./n");
exit(-1);
}
}

ret=fclose(ftp_fp);
if(ret==EOF) {
fprintf(stderr,"fclose %s error./n",UP_FTP_NAME);
exit(-1);
}

ret=sprintf(ftp_cmd,"ftp -s:%s",UP_FTP_NAME);
if(ret<0) {
fprintf(stderr,"sprintf ftp_cmd error./n");
exit(-1);
}

// popen(ftp_cmd); // for system
system(ftp_cmd);

unlink(UP_FTP_NAME);
//----------------------------------------------------------------------
}

int main(void) {
int i;

struct tm *p1;
time_t timep;

char openfile1[128];
char openfile2[128];

char *openfile[COUNT];

timep=time(NULL);
p1=localtime(&timep);

/*
sprintf(openfile[0],"d://oracle_exp_bak//bwdasic-%d-%02d-%02d.dmp",1900+p1->tm_year,(1+p1->tm_mon),p1->tm_mday);
sprintf(openfile[1],"e://orabak//bwdasic-%d-%02d-%02d.dmp",1900+p1->tm_year,(1+p1->tm_mon),p1->tm_mday);
*/
sprintf(openfile1,"d://oracle_exp_bak//bwdasic-%d-%02d-%02d.dmp",1900+p1->tm_year,(1+p1->tm_mon),p1->tm_mday);
sprintf(openfile2,"e://orabak//bwdasic-%d-%02d-%02d.dmp",1900+p1->tm_year,(1+p1->tm_mon),p1->tm_mday);
// printf("%s/n",openfile1);

// handle_file(openfile);

// handle file cmd
handle_file(openfile1);

handle_file(openfile2);

// execute ftp up cmd
ftp_up_cmd();

unlink(UP_FTP_NAME);

exit(0);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐