⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 例3-7.c

📁 这是用C编写的文件合并与分割的源代码
💻 C
字号:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

long filesize(FILE *stream); /* 求文件的长度 */

int main()
{
   int i;
   char inf1,inf2,outf;  /* 用户输入的文件路径 */
   FILE *in1, *in2, *out;  /*in1,in2为待合并的文件,out为合并后的文件 */
   unsigned long in1fileLength,in2fileLength;
   char *buf1, *buf2;

        printf("------------------------------------------------\n");
        printf(">  Please input parameter:\n");
        printf(">  test <InputFileName1><InputFileName2> <OutputFileName>:\n");
        printf("------------------------------------------------\n");  

   printf("These are the  command-line arguments passed to"
          " main:\n\n");

   scanf("%s",inf1);            /* 用户输入待合并的文件1 */
   if ((in1 = fopen(inf1, "rb")) == NULL)
   {
      printf("Cannot open input file1.\n");
      exit(1);
   } 
   else
   	  printf("file1 is opened,please input the file2\n");
   scanf("%s",inf2);                    /* 待合并文件2 */
   if ((in2 = fopen(inf2, "rb")) == NULL)
   {
      printf("Cannot open input file2.\n");
      exit(1);
   } 
   else 
   	  printf("file2 is opened,please input the loctation after united\n"); 
   scanf("%s",outf);                     /* 经过合并操作后存放的路径 */
   if ((out = fopen(outf, "wb"))== NULL)
   {
      printf("Cannot open output file.\n");
      exit(1);
   }  
 
   
   in1fileLength = filesize(in1);    /* 文件1的长度 */
   in2fileLength = filesize(in2);    /* 文件2的长度 */

   buf1 = (char *)malloc(in1fileLength);  /* 读出放入缓冲区中 */
   buf2 = (char *)malloc(in2fileLength);
   
   fread(buf1,in1fileLength, 1, in1);
   fread(buf2,in2fileLength, 1, in2);
   
   fwrite(buf1, in1fileLength, 1, out);  /* 写入out文件中 */
   fwrite(buf2, in2fileLength, 1, out);
   printf("unit is successful!,and the loctation is %s",outf);

   fclose(in1);       /* 关闭文件 */
   fclose(in2);
   fclose(out);
   
   return 1;
}


long filesize(FILE *stream)    /* 求文件长度的函数 */
{
   long curpos1, curpos2,length;
   
   fopen(stream,"rb");
   curpos1= ftell(stream);
   fseek(stream, 0L, SEEK_END);
   curpos2= ftell(stream);  
   length=curpos2-curpos1;
   fseek(stream, curpos1, SEEK_SET);
   return length;
   fclose(stream);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -