File Copy binary plate

Source: Internet
Author: User

Two methods of replication:

FILE *src = fopen ("Src.txt", "R");

FILE *dest = fopen ("Dest.txt", "w");

Char buf[1024]={0};

while (!feof (SRC)) {

size_t size = fread (buf,1,sizeof (buf), SRC);

Fwrite (buf,1,size,dest);

}

Using the stat function

#include <sys/stat.h>

Struct Stat St;

Stat ("Src.txt", &st);

Char buf[] = malloc (st.st_size);

Fread (BUF,ST.ST_SIZE,1,SRC);

Fwrite (buf,st.st_size,1,dest);

Remember to release the memory

Free (BUF);

Both of these are possible, and the second is to change time by space. Because he does it once, so it's very fast and efficient. But the file is not very large, so you can make a judgment, if the file is within the scope of the use of this method to replicate, the use of space in exchange for time.

The first one is a one-take-all notation that can be copied, but slower than the second. The advantage is that most of the copies can be done.

But we generally have the same buffer, do not do too much, do not do too small.

Too large can cause a waste of the buffer, too small to move too many times.

So after a long time such an attempt and consideration decided to set this number to 64*1024. also 64k . The General memory page is the size of 64k.

A simple version of a file copy is given below. Of course, the stat function can be used and no recursion has been made before the test is too large.

Try the file Size section first.

Main.c

#include <stdio.h>#include<sys/stat.h>#pragmaWarning (disable:4996)#defineMAXSIZE4FUNC2 1038684887//a zip file of size 990Mb that has been detected. This number is 1 billion bytes, so you can feel the files within 1G. #define_1G 1073741824//This is a computed 1g=1073741824 byte. #include"Func1.h"#include"func2.h"/** can be made by double-clicking the execution version.    A single file, and iterate? Determine whether the file or directory is currently*/voidWorkflowConst int,Const Char**);voidJudgeentrance (Const Char**ARGV,ConstFile *src, File *dest);intMainintargcChar**argv)    {Workflow (ARGC,ARGV); //FILE *src = fopen ("A.txt", "R"); //FILE *dest = fopen ("2b.txt", "w"); //copy1 (SRC, dest); //copy2 ("A.txt", SRC, dest); //fclose (SRC); //fclose (dest);    return 0;}voidWorkflowConst intargcConst Char**argv) {    if(ARGC <3) {printf ("too few parameters, three parameters required"); }    Else if(argc>3) {printf ("too many parameters, three parameters required"); }    Else{FILE*SRC = fopen (argv[1],"RB"); FILE*dest = fopen (argv[2],"WB");        Judgeentrance (argv,src,dest);        Fclose (SRC);    Fclose (dest); }}voidJudgeentrance (Const Char**ARGV,ConstFILE *src,file *dest) {    structStat St; Stat (argv[1], &St); if(St.st_size < _1G) {//The practice of space-changing timeCopy2 (argv[1], SRC, dest); }    Else{//the practice of a wildcardcopy1 (SRC, dest); } printf ("%d", st.st_size);}

Func1.h

#ifndef _func1_h #define _func1_h<stdio.h>void copy1 (file *src, file *dest); #endif

func1.c

#include <stdio.h>void copy1 (file *src, file *dest) {    char buf[1024x768  0  };      while (! feof (SRC)) {        1sizeof(buf), SRC);         1 , size, dest);}    }

Func2.h

#ifndef _func2_h #define _func2_h<stdio.h>void copy2 (constcharconst File *src, File *dest); #endif

Func2.c

#include <sys/stat.h><stdio.h>void copy2 (constcharconst File *src, File *dest) {    struct  stat st;     &St);     Char malloc (st.st_size);     1 , SRC);     1 , dest);      Free (BUF);}

File Copy binary plate

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.