今天补习的几个C代码,后续整理下

#include <stdio.h>
#include <stdlib.h>


int main()
{
  FILE *fp;
  char ch;
  
  char pRetMsg[128];
  int msg_len=0;
  
  memset(pRetMsg, 0, sizeof(pRetMsg));
  
  if((fp=fopen("test.txt","r"))==NULL)
  {
        printf("Error!\n");
        exit(0);
  }
  
  if(fgets(pRetMsg,sizeof(pRetMsg),fp)!=NULL)
  {
       printf("FTP cmd Fail...:%s",pRetMsg); //print all info
       
       if(strncmp("cannot connect to remote host",pRetMsg+8,strlen("cannot connect to remote host"))==0)
       {
           printf("FTP ServerIp Error.\n");
       }
       else if(strncmp("unexpected server response to PASS",pRetMsg+8,strlen("unexpected server response to PASS"))==0)
       {
           printf("FTP User or passwd error.\n");
       }
       else if(strncmp("write error: No space left on device",pRetMsg+8,strlen("write error: No space left on device"))==0)
       {
           printf(" No space left on device\n");
       }
       else if(strncmp("unexpected server response to RETR",pRetMsg+8,strlen("unexpected server response to RETR"))==0)
       {
           printf("FTP Download path error\n");
       }
       else if(strncmp("can't open",pRetMsg+8,strlen("can't open"))==0)
       {
           printf("FTP upload file not exit\n");
       }
       else{
          printf("Other ftp fail reason,see ftp.log");
       }

  }
  else{
   printf("Content of file is null,ftp cmd success!!!\n");
  }
  
  fclose(fp);
   return 0;
}

 

 

 

 

#include "stdio.h"
#include <string.h>


int main()
{
     char userName[16];
     char passwd[16];
     char ftp_server_ip[16];
     char srcPath[128];
     char str[128];
    sprintf(str,"%s","zhangling@192.168.240.50/FTP_Download/");
    sscanf(str,"%[^@]",userName);
    sscanf(str,"%*[^@]@%[^/]",ftp_server_ip); //need test
    sscanf(str,"%*[^/]%s",srcPath);

    printf("userName=%s\n",userName);
    printf("ftp_server_ip=%s\n",ftp_server_ip);
    printf("srcPath=%s\n",srcPath);
    
    return 0;
}
#include "stdio.h"
#include <string.h>
/* just get lastest info */
int _System(const char * cmd, char *pRetMsg, int msg_len)
{
    FILE *fp;
    char *p = NULL;
    int res = -1;
    if (cmd == NULL || pRetMsg == NULL || msg_len < 0)
    {
        printf("Param Error!\n");
        return -1;
    }
    if ((fp = popen(cmd, "r") ) == NULL)
    {
        printf("Popen Error!\n");
        return -2;
    }
    else
    {
        memset(pRetMsg, 0, msg_len);
        //get lastest result
        while(fgets(pRetMsg, msg_len, fp) != NULL)
        {
            printf("Msg@_@:%s",pRetMsg); //print all info
        }
 
        if ( (res = pclose(fp)) == -1)
        {
            printf("close popenerror!\n");
            return -3;
        }
        pRetMsg[strlen(pRetMsg)-1] = '\0';
        return 0;
    }
}
 
int main()
{
    //test cmd
    char cmd[256];
    memset(cmd,0,sizeof(cmd));
    sprintf(cmd,"%s","ls");
    char a8Result[256] = {0};
    int ret = 0;
    ret  = _System(cmd, a8Result, sizeof(a8Result));
    printf("ret = %d \n   a8Result = %s\n   length = %d \n", ret, a8Result, strlen(a8Result));
    return 0;
}

 

posted @ 2018-07-25 17:35  琳麻雀  阅读(200)  评论(0编辑  收藏  举报