Skip to content

Instantly share code, notes, and snippets.

@oblerion
Last active December 14, 2023 12:07
Show Gist options
  • Save oblerion/55a0673941243bcab6e9d82312bf8bde to your computer and use it in GitHub Desktop.
Save oblerion/55a0673941243bcab6e9d82312bf8bde to your computer and use it in GitHub Desktop.
cat binairy file in c
#ifndef BINAIRY_CAT_H
#define BINAIRY_CAT_H
#include <stdlib.h>
#include <stdio.h>
bool FileExists(char* pfile)
{
FILE* fic = fopen(pfile,"r");
if(fic!=NULL)
{
fclose(fic);
return true;
}
return false;
}
int GetFileLenght(char* pfile)
{
int size=0;
FILE* fic = fopen(pfile,"r");
if(fic!=NULL)
{
fseek(fic,SEEK_END,0);
size = ftell(fic);
fclose(fic);
}
return size;
}
void binairy_cat(char* pfile,char* pfile2,const char* pout)
{
if(FileExists(pfile) && FileExists(pfile2))
{
FILE* fout = fopen(pout,"wb");
FILE* ffile = fopen(pfile,"rb");
const int filesize = GetFileLength(pfile);
char buf1[filesize];
fread(buf1,filesize,1,ffile);
fclose(ffile);
FILE* ffile2 = fopen(pfile2,"rb");
const int filesize2 = GetFileLength(pfile2);
char buf2[filesize2];
fread(buf2,filesize2,1,ffile2);
fclose(ffile2);
fwrite(buf1,filesize,1,fout);
fwrite(buf2,filesize2,1,fout);
fclose(fout);
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment