[英]C - Bubble Sort Program binary file
我試圖在不使用任何數組的情況下在二進制文件中創建冒泡排序程序,而不是使用fseek和fwrite函數。
這是我的代碼:
typedef struct{ //Films
short int year;
char title[LEN_NAME];
Genero_t gendre;
float rateIMDB;
}Ficha_t;
FILE * fd;
fd = fopen(name,"r+b");
Ficha_t aux1;
Ficha_t aux2;
int i,j,len;
if (fd != NULL)
{
rewind(fd);
fseek(fd, 0, SEEK_END);
len=ftell(fd);
rewind(fd);
for(i=0;i<len;i++);
{
for(j=0;j<len-1;j++)
{
fread(&aux1,sizeof(Ficha_t),1,fd);
fread(&aux2,sizeof(Ficha_t),1,fd);
if(strcmp(aux1.title,aux2.title)<0)
{
fseek(fd,-sizeof(Ficha_t)*2,SEEK_SET); //returning 2 positions for overwriting
fwrite(&aux2, sizeof(Ficha_t), 1, fd);
fwrite(&aux1, sizeof(Ficha_t), 1, fd);
fseek(fd,-sizeof(Ficha_t),SEEK_SET); //returning 1 position for checking again with the next film
}
}
}
}
由於正在顯示相同的2部電影,因此無法正常工作,我在哪里錯? 我能做什么?
如評論中所述,您想要:
len=ftell(fd) / sizeof(Ficha_t);
獲取文件中的記錄數。
如果您嘗試使用當前位置偏移量進行搜索,請使用SEEK_CUR
而不是SEEK_SET
。
我會為ftell(fd) / sizeof(Ficha_t)
添加一堆printfs,以便您可以看到它正確地完成了所有操作。 另外,您可能正在檢查正在調用的函數的返回值(在嘗試從頭開始並在此處讀/寫時,它們可能返回了錯誤)
具體代碼示例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
void fbubble_sort(const char *filename, size_t rec_size, bool (*needSwap)(const void *, const void *)){
FILE *fp = fopen(filename, "r+b");
if(!fp){
perror("fopen, r+");
exit(EXIT_FAILURE);
}
void *first = malloc(rec_size);
void *second = malloc(rec_size);
if(!first || !second){
perror("malloc");
exit(EXIT_FAILURE);
}
enum { NO_SWAP = -1};
long last_swap_pos;
do {
rewind(fp);
long pos_1 = 0;//first record position
last_swap_pos = NO_SWAP;
if(0==fread(first, rec_size, 1, fp))
break;
while((pos_1 <= last_swap_pos || last_swap_pos == NO_SWAP) && 1==fread(second, rec_size, 1, fp) ){
if(needSwap(first, second)){
fseek(fp, pos_1, SEEK_SET);
fwrite(second, rec_size, 1, fp);
fwrite(first, rec_size, 1, fp);
fflush(fp);
last_swap_pos = pos_1;
} else {
//exchange buffer
void *temp = first;
first = second;
second = temp;
}
pos_1 += rec_size;
}
}while(last_swap_pos != NO_SWAP);
free(first);free(second);
fclose(fp);
}
#define DATA_FILE "test.dat"
#define GREATERi(type, member)\
bool greater_i(const void *first, const void *second){\
return ((type*)first)->member > ((type*)second)->member;\
}\
/**/
#define GREATERs(type, member)\
bool greater_s(const void *first, const void *second){\
return strcmp((type*)first)->member, ((type*)second)->member) > 0;\
}\
/**/
#define LESSi(type, member)\
bool less(const void *first, const void *second){\
return ((type*)first)->member < ((type*)second)->member;\
}\
/**/
typedef struct {
unsigned v;
} Record;
GREATERi(Record, v)
int main(void){
void make_test_data(void);
void print_test_data(void);
make_test_data();
print_test_data();
fbubble_sort(DATA_FILE, sizeof(Record), greater_i);
print_test_data();
}
void make_test_data(void){
FILE *fp = fopen(DATA_FILE, "wb");
if(!fp){
perror("fopen, w");
exit(EXIT_FAILURE);
}
Record rec;
for(int i = 0; i < 100; ++i){
rec.v = rand() % 100;
fwrite(&rec, sizeof(rec), 1, fp);
}
fclose(fp);
}
void print_test_data(void){
FILE *fp = fopen(DATA_FILE, "rb");
if(!fp){
perror("fopen, r");
exit(EXIT_FAILURE);
}
Record rec;
int cnt = 0;
while(fread(&rec, sizeof(rec), 1, fp)){
if(cnt)
putchar(' ');
printf("%2u", rec.v);
if(++cnt == 10){
putchar('\n');
cnt = 0;
}
}
putchar('\n');
fclose(fp);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.