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

📄 例3-6.c

📁 这是一个关于文件的分割和合并的程序
💻 C
字号:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

long filesize(FILE *stream);

int main()
{
   int i; 
   char inf,outf1,outf2;
   FILE *in, *out1, *out2;
   unsigned long infileLength,out1fileLength,out2fileLength;
   char *endptr, *buf;
   
   printf("------------------------------------------------\n");
   printf(">  Please input parameter:\n");
   printf(">  test <InputFileName> <OutputFileName>:\n");
   printf("------------------------------------------------\n");
   printf("These are the  command-line arguments passed to"
          " main:\n\n"); 
          
   printf("Please input the seperated file\n");            /* 用户输入要分解的文件 */                                                     
   scanf("%s",inf);
   
   if ((in = fopen(inf, "rb"))==NULL)
   {
      printf("Cannot open input file.\n");
      exit(1);
   } 
   else
   	  printf("The seperated file is opened,please input the file1 after seperate\n");
   scanf("%s",outf1);                    /* 分解后的文件1*/
   if ((out1 = fopen(out1, "wb")) == NULL)
   {
      printf("Cannot open output file1.\n");
      exit(1);
   } 
   else 
   	  printf("outfile1 is opened,please input the file2 after seperate\n"); 
   scanf("%s",outf2);                     /* 分解后的文件2*/
   if ((out2 = fopen(outf2, "wb"))== NULL)
   {
      printf("Cannot open output file2.\n");
      exit(1);
   }  
   else 
   	  printf("outfile2 is opened");                                                 
                                                        
   infileLength=filesize(in);      /* 元文件长度 */                                               
                                                        
   printf("\nThe size of the outfile1 is:\n");
   scanf("%d",&out1fileLength);    /* 用户要求分解到的文件长度 */
  
 
   if(out1fileLength > infileLength)
   {
      printf("[ERROR]:out1fileLength is too large, Please try to allocate again!\n");
      exit(1);
   }

   buf = (char *)malloc(infileLength);  
   out2fileLength = infileLength - out1fileLength;  
   fread(buf, infileLength, 1, in);   
   fwrite(buf, out1fileLength, 1, out1);   
   fwrite((char*)(buf+out1fileLength), out2fileLength, 1, out2);   
   out1fileLength = filesize(out1);
   printf("out1fileLength=%ld\n",out1fileLength);   
   out2fileLength = filesize(out2);
   printf("out2fileLength=%ld\n",out2fileLength);   

   fclose(in);      /*关闭文件 */
   fclose(out1);
   fclose(out2);
}

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 + -