Replication of single-file content in Linux

Source: Internet
Author: User
Tags fread save file

general methods for file content replication:
    • Open up a space, constantly read the contents of the file and write to another file, this method is safe, generally within the maximum range allowed by the type is safe, the disadvantage is that the time to copy the content is long
    • Copy the contents of the file at once, this method must first obtain the current copy of the file content size, and then open up the same size as the file content of the memory space, usually for security, you must make the size plus 1.
to read the contents of a file
    • Open the copied file (Open/fopen)
    • Read file contents (Read/fread)---> The content can be read directly when using the system IO (open), but when using standard IO (fopen), the next step must be made
    • When standard IO reads the contents of the file, it must first obtain the size of the file content, usually using fseek and Ftell, System IO is not
    • Write to another file to save (write/fwrite)
    • Close an open file
the specific Operation(This article operates with standard IO, considering portability)
    • Use fopen to open the copied file and create a saved file and decide whether to open or create a successful
    • Use fseek and Ftell to get the content size of the currently copied file filesize;
    • Open up a memory space, it is recommended to use dynamic open-up methods, such as Mallco,calloc,new
    • Use Fread to read the contents of the copied file, using Fwrite to write into the saved file
    • Use Fclose to close the two files above
Code Display
1#include <stdio.h>2 3 intMainintargcChar*argv)4 {5     //Open the copied file in a read-only manner6FILE *SRCFD = fopen ("./source.txt","R");7     if(NULL = =srcfd)8     {9printf"Open source file failed\n");Ten         return-1; One     } A  -     //create a non-existent file in read-write mode -FILE *dstfd = fopen ("./save.txt","w+"); the     if(NULL = =dstfd) -     { -printf"Open Save File failed\n"); -         return-1; +     } -  +     //to get the file size operation AFseek (SRCFD,0, seek_end);//move the cursor to the end of the file at     LongFileSize = Ftell (SRCFD);//get the size of a file -Fseek (SRCFD,0, Seek_set);//restores the cursor to the beginning of the file -  -     //open up a new space (dynamic opening) -     Char*databuf = (Char*)calloc(1, filesize+1); -     if(NULL = =databuf) in     { -printf"calloc a memory failed\n"); to         return-1; +     } -  the     //read the contents of a file *     LongRetsize = Fread (Databuf,1, FILESIZE,SRCFD); $     if(Retsize! =fileSize)Panax Notoginseng     { -printf"Read File context failed\n"); the         return-1; +     } A  the     //write in Save file +Retsize = fwrite (Databuf,1, FILESIZE,DSTFD); -     if(Retsize! =fileSize) $     { $printf"Write context in file failed\n"); -         return-1; -     } the  -     //close files and free up memory spaceWuyi fclose (SRCFD); the fclose (DSTFD); -  Wu      Free(DATABUF); -  About     return 0;  $}

Replication of single-file content in Linux

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.