"C language Programming" uses C language to implement the merging of filegroups

Source: Internet
Author: User

The use of C language file operations to achieve the merging of filegroups, the following is the code, the process is written in the comments, the main idea is to get the length of the file one by one, and then copy the whole block of the file, enter it into the end of the target file:

#include <stdio.h>#include <conio.h>#include <stdlib.h>//merge FilegroupsvoidMergefiles (Char* * Sfiles,intNfilecount,Char* target);intMain () {Char* * p= (Char**)malloc( -); p[0]="1.txt";//Contents: Moonlight Before the bed,p[1]="2.txt";//Inside content: Suspicion is ground frostp[2]="3.txt";//inside content:.     Char* target="4.txt"; Mergefiles (P,3, target);//merge three files, result output to 4.txt    //Open 4.txt and output the contents to the screenfile* file1 = fopen ("4.txt","R");CharC//One character to output the contents of the file to the screen one character at a     while((C=fgetc (file1))!=eof) {printf("%c", c); } getch ();return 0;}//merge FilegroupsvoidMergefiles (Char* * Sfiles,intNfilecount,Char* _target) {inti =0;//Current file, target fileFILE *current,*target;intLength =0;Char* s; target = fopen (_target,"WB");//Open the target file in writable binary mode     for(i =0; i < Nfilecount; i++)//traverse the source filegroup according to the number of files{current = fopen (Sfiles[i],"RB");//Open the current source file in binary read-only modeFseek (Current,0, seek_end);//Navigate to the end of the current source fileLength = Ftell (current);//Gets the position of the current source file pointer, which gets the file length        if(!length)return; Fseek (Current,0, Seek_set);//Navigate to the beginning of the current source files = (Char*)malloc(length);//Read the buffer of the source fileFread (s),1, length,current);//Read the contents of the source file to the bufferFwrite (s),1, Length,target);//write the contents of the buffer to the target fileFclose (current);//Close the current source file and start reading the next source file} fclose (target);//Close the target file}

Output Result:

"C language Programming" uses C language to implement the merging of filegroups

Related Article

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.