Last active
August 29, 2015 14:23
-
-
Save alex-pat/442b41fcfcc103fe1d07 to your computer and use it in GitHub Desktop.
Insert sort of binary file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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