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