#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Posts
    13
    Rep Power
    0

    Swapping lines in binary file not working


    Ok I am trying to make a generic function that can sort a file without an array.

    The problem is it does not swap right.==> it ruins the information of the file.

    I am currently trying to swap 2 structures only before trying to sort.

    I am not sure whether to use void* or char* (in function body)
    I tried to use char* as this is the smallest type but after it failed I tried void*....though I think it should b char* but yet again nothing work so far so I am not certain.

    eventually in order to make sure it works I tried the structure's type itself .....

    this is the code:
    Code:
    	student_t* aaa1=(student_t*)malloc(sizeof(student_t));
    	student_t* aaa2=(student_t*)malloc(sizeof(student_t));
    
    	//=====reading from file=======
    	f=fopen(filename,"rb");
    
    	fseek(f,typeSize*2,SEEK_SET);
    	fread(aaa1,sizeof(student_t),1,f);
    
    	fseek(f,typeSize*1,SEEK_SET);
    	fread(aaa2,sizeof(student_t),1,f);
    
    	fclose(f);
    	//========END reading================
    
    
    	//========writing to file=================
    	f=fopen(filename,"wb");
    
    	fseek(f,typeSize*2,SEEK_SET);
    	fwrite(aaa2,sizeof(student_t),1,f);
    
    	fseek(f,typeSize*1,SEEK_SET);
    	fwrite(aaa1,sizeof(student_t),1,f);
    
    	fclose(f);
    	//=========END writing================

    and this is the output (code for output is not included but I believe it is clear enough I simply read into array of structures print it and then read again after function finishes working)

    Code:
    Before:
    -------
    name: stud4 id= 1  avg= 99.600000
    
    name: stud5 id= 2  avg= 100.000000
    
    name: stud3 id= 3  avg= 99.500000
    
    name: stud1 id= 4  avg= 80.000000
    
    name: stud2 id= 5  avg= 91.000000
    
    
    
    
    After:
    -------
    name:  id= 0  avg= 0.000000
    
    name: stud3 id= 3  avg= 99.500000
    
    name: stud5 id= 2  avg= 100.000000
    
    name: stud1 id= 4  avg= 80.000000
    
    name: stud2 id= 5  avg= 91.000000

    p.s I tried using r+b or w+b but it failed so I separately read and write to file



    this is how I create the file:

    Code:
    FILE* f = fopen("students.bin", "wb");
    if (f == NULL)
    {
    	printf("Failed opening the file. Exiting!\n");
    	return;
    }
    
    fwrite(&studs,sizeof(student_t),SIZE,f);					
    fclose(f);

    please notice SIZE is defined as 5.

    studs declaration:
    Code:
    student_t studs[SIZE]={  {"stud4",1,99.6} , {"stud5",2,100.0},{"stud3",3,99.5},{"stud1",4,80},{"stud2",5,91}};
    this is how I call the function (sort):
    Code:
    sort("students.bin",sizeof(student_t),SIZE,compareStudentByAVG);

    and this is the function's signature:

    Code:
    void sort(char* filename,int typeSize,int numOfStructs, int (*compare)(void*, void*))

    please ignore the function pointer.
    (as for it is not called at the moment)
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    I would suggest you check your attempt with "r+b" again, because this is what you want (to open a file for update, preserving the current contents).

    Code:
    $ cat bar.c
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <assert.h>
    
    typedef struct {
      char  name[20];
      int   id;
      float grade;
    } student_t;
    student_t init[] = {
      { "one",    0x11111111, 1.0 },
      { "two",    0x22222222, 2.0 },
      { "three",  0x33333333, 3.0 },
      { "four",   0x44444444, 4.0 },
      { "five",   0x55555555, 5.0 },
    };
    
    int main ( ) {
      const char *filename = "test.dat";
      FILE *fp;
      fp = fopen(filename,"wb");
      if ( !fp ) { perror("oops");exit(1); }
      fwrite(init,sizeof(init[0]),5,fp);
      fclose(fp);
      system("od -Ax -t x1z test.dat");
      student_t* aaa1=malloc(sizeof(student_t));
      student_t* aaa2=malloc(sizeof(student_t));
      size_t  typeSize = sizeof(student_t);
    
      //=====reading from file=======
      FILE *f=fopen(filename,"rb");
    
      fseek(f,typeSize*2,SEEK_SET);
      fread(aaa1,sizeof(student_t),1,f);
    
      fseek(f,typeSize*1,SEEK_SET);
      fread(aaa2,sizeof(student_t),1,f);
    
      fclose(f);
      //========END reading================
    
    
      //========writing to file=================
      f=fopen(filename,"r+b");
    
      fseek(f,typeSize*2,SEEK_SET);
      fwrite(aaa2,sizeof(student_t),1,f);
    
      fseek(f,typeSize*1,SEEK_SET);
      fwrite(aaa1,sizeof(student_t),1,f);
    
      fclose(f);
      //=========END writing================
      system("od -Ax -t x1z test.dat");
      return 0;
    }
    $ ./a.out 
    000000 6f 6e 65 00 00 00 00 00 00 00 00 00 00 00 00 00  >one.............<
    000010 00 00 00 00 11 11 11 11 00 00 80 3f 74 77 6f 00  >...........?two.<
    000020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  >................<
    000030 22 22 22 22 00 00 00 40 74 68 72 65 65 00 00 00  >""""...@three...<
    000040 00 00 00 00 00 00 00 00 00 00 00 00 33 33 33 33  >............3333<
    000050 00 00 40 40 66 6f 75 72 00 00 00 00 00 00 00 00  >..@@four........<
    000060 00 00 00 00 00 00 00 00 44 44 44 44 00 00 80 40  >........DDDD...@<
    000070 66 69 76 65 00 00 00 00 00 00 00 00 00 00 00 00  >five............<
    000080 00 00 00 00 55 55 55 55 00 00 a0 40              >....UUUU...@<
    00008c
    000000 6f 6e 65 00 00 00 00 00 00 00 00 00 00 00 00 00  >one.............<
    000010 00 00 00 00 11 11 11 11 00 00 80 3f 74 68 72 65  >...........?thre<
    000020 65 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  >e...............<
    000030 33 33 33 33 00 00 40 40 74 77 6f 00 00 00 00 00  >3333..@@two.....<
    000040 00 00 00 00 00 00 00 00 00 00 00 00 22 22 22 22  >............""""<
    000050 00 00 00 40 66 6f 75 72 00 00 00 00 00 00 00 00  >...@four........<
    000060 00 00 00 00 00 00 00 00 44 44 44 44 00 00 80 40  >........DDDD...@<
    000070 66 69 76 65 00 00 00 00 00 00 00 00 00 00 00 00  >five............<
    000080 00 00 00 00 55 55 55 55 00 00 a0 40              >....UUUU...@<
    00008c
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo