Skip to content

Instantly share code, notes, and snippets.

@alex-pat
Last active August 29, 2015 14:23
Show Gist options
  • Save alex-pat/442b41fcfcc103fe1d07 to your computer and use it in GitHub Desktop.
Save alex-pat/442b41fcfcc103fe1d07 to your computer and use it in GitHub Desktop.
Insert sort of binary file
void bininsert (FILE* f)
{
if (!f) return;
fseek(f, 0, 2);
fpos_t i, j, n = ftell(f);
int tmp, sd;
for ( i = sizeof(int); i < n; i += sizeof(int) )
{
j = i - sizeof(int);
fsetpos(f, &i);
fread(&tmp, sizeof(int), 1, f);
for ( ; j >= 0 ; j -= sizeof(int))
{
fsetpos(f, &j);
fseek(f,0,1);
fread(&sd, sizeof(int), 1,f);
if (sd <= tmp) break;
fseek(f,0,1);
fwrite(&sd,sizeof(int), 1,f);
}
//if ( i == ftell(f)) continue;
fseek(f,0,1);
if ( j < 0 ) rewind(f);
fwrite (&tmp, sizeof(int), 1, f);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment