• 今天补习的几个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!
    ");
            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.
    ");
           }
           else if(strncmp("unexpected server response to PASS",pRetMsg+8,strlen("unexpected server response to PASS"))==0)
           {
               printf("FTP User or passwd error.
    ");
           }
           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
    ");
           }
           else if(strncmp("unexpected server response to RETR",pRetMsg+8,strlen("unexpected server response to RETR"))==0)
           {
               printf("FTP Download path error
    ");
           }
           else if(strncmp("can't open",pRetMsg+8,strlen("can't open"))==0)
           {
               printf("FTP upload file not exit
    ");
           }
           else{
              printf("Other ftp fail reason,see ftp.log");
           }
    
      }
      else{
       printf("Content of file is null,ftp cmd success!!!
    ");
      }
      
      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
    ",userName);
        printf("ftp_server_ip=%s
    ",ftp_server_ip);
        printf("srcPath=%s
    ",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!
    ");
            return -1;
        }
        if ((fp = popen(cmd, "r") ) == NULL)
        {
            printf("Popen Error!
    ");
            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!
    ");
                return -3;
            }
            pRetMsg[strlen(pRetMsg)-1] = '';
            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 
       a8Result = %s
       length = %d 
    ", ret, a8Result, strlen(a8Result));
        return 0;
    }
  • 相关阅读:
    如何将javaweb项目部署到阿里云服务器上
    解决ueditor配置文件第一行报错及乱码问题
    解决web.xml中<async-supported>true</async-supported>报错问题
    解决建完Maven项目没有main文件夹问题
    CentOS常用命令之搜索文件
    CentOS常用命令之创建删除剪切链接
    Jupyter notebook: TypeError: __init__() got an unexpected keyword argument 'io_loop 问题
    编译分布式并行版caffe(Open MPI)教程
    树状数组
    马拉车算法
  • 原文地址:https://www.cnblogs.com/secondtononewe/p/9367271.html
Copyright © 2020-2023  润新知