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

来源:互联网 发布:mac英雄联盟 编辑:程序博客网 时间:2024/05/02 04:17
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <time.h>#define UP_FTP_NAME "up_ftp.txt"#define COUNT 2int 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 envchar 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 systemsystem(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 cmdhandle_file(openfile1);handle_file(openfile2);// execute ftp up cmdftp_up_cmd();unlink(UP_FTP_NAME);exit(0);}
原创粉丝点击