zoukankan      html  css  js  c++  java
  • 用C语言怎么实现复制自己

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


    int main(int argc, char *argv[])
    {
    char str[80];
    int i = 0;
    for(i = 0; i < 5; i++)
    {
    sprintf(str, "cp %s %d%s", argv[0], i, ".out");  //在WIN下用copy取代cp
    system(str);
    }
    //printf("%s ", str);
    return 0;

    }


    下边是自己动手写的copy函数

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


    void copy_file(char *SrcFileName, char *DesFileName)
    {
    char buf[1024];
    int read_len = 0;
    FILE *in = NULL;
    FILE *out = NULL;


    if((in = fopen(SrcFileName, "rb")) == NULL)
    {
    printf("Open %s failure. ", SrcFileName);
    exit(1);
    }


    if((out = fopen(DesFileName, "wb")) == NULL)
    {
    printf("Open %s failure. ", SrcFileName);
    exit(1);
    }


    while((read_len = fread(buf, sizeof(char), 1024, in)) != 0) {
    fwrite(buf, sizeof(char), read_len, out);
    }

    fclose(in);
    fclose(out);
    }
    int main(int argc, char *argv[])
    {
    char str[80];
    int i = 0;
    char *find_result;


    for(i = 0; i < 5; i++)
    {
    sprintf(str, "%d%s", i, ".out");
    find_result = strrchr(argv[0], '/');
    if (strcmp(str, find_result + 1) !=0 )
    {
    copy_file(argv[0], str);
    }
    }
    printf("%s ", str);
    return 0;
    }


  • 相关阅读:
    474. Ones and Zeroes
    [LeetCode]464. Can I Win
    413. Arithmetic Slices
    numpy学习(布尔型索引)
    numpy学习(数组和标量之间的运算切片)
    numpy学习(数组的定义及基础属性)
    关于静态显示游标的遍历
    关于oracle的数组
    shutil模块
    开源库(不定义更新)
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/4259451.html
Copyright © 2011-2022 走看看