Reading various parts of binary file in c -


i writing program read specific parts of binary file , having trouble getting read right lengths of binary @ right locations in file. i've been trying figure out i'm doing wrong time no success.

here code looks like

int project(char * rel, char * atr){  takes in name of relation in , name of attribute      char tmpstr[max_len+5], tmpstr2[max_len+5], attr[max_len+5], type[max_len+5], names[max_len+5];//attr attribute read pass of fscanf()     int numbytes = 0, numtups = 0, nummove = 0, skip = 0, = 0;    //the length in bytes of atr, number of tuples read     file* f1;     file* f2;     strcpy(tmpstr, rel);     strncat(tmpstr,".sch", 4); // appends '.sch' extension file name     if((f1 =(file*)fopen(tmpstr, "r")) == null){  // opens "insertfilenamehere.sch"         return -1;     }     if(fscanf(f1, "%d", &numattr) != 1){         return -1;     }     strcpy(tmpstr2, rel);     strncat(tmpstr2,".dat", 4); // appends '.dat' extension file name     if((f2 =(file*)fopen(tmpstr2, "rb")) == null){  // opens "insertfilenamehere.dat"         return -1;     }      fscanf(f1, "%s", attr);       fscanf(f1, "%1s", type);     fscanf(f1, "%d", &numbytes);     while((strcmp(attr, atr) != 0) || numattr == 0){ //while attr = atr or have run out of attributes scan, keep scanning         skip = skip + numbytes*2;         fscanf(f1, "%s", attr);         fscanf(f1, "%1s", type);         fscanf(f1, "%d", &numbytes);         numattr--;         if(numattr == 0){             return -2;         }         fseek(f2, skip, seek_cur);     }     nummove = tuplen(rel) - numbytes*2;     numtups = count(rel);     printf("%d\n", nummove);     while(i < numtups){         fread(names, 1, numbytes*2, f2);         fprintf(stdout, "%s\n", names);         fseek(f2, skip, seek_cur);         i++;     }     puts("\n");     fclose(f1);     fclose(f2);     return 1;  } 

the files have been using test code follows:

students.sch: first line of file amount of subsequent lines in file. on each line string followed type of data read binary file, number of bytes read binary file. file used tell program how read binary file.

5 name   s  25 major  s  4 minor  s  4 totcr   4 majcr   4 

students.dat: file contains information read

536d 6974 682c 526f 6265 7274 0000 0000 0000 0000 0000 0000 0050 5359 0043 5349 0000 0000 3900 0000 2757 6f6f 6473 2c4a 616e 6500 0000 0000 0000 0000 0000 0000 0000 4353 4900 4255 5300 0000 0061 0000 0044 5261 6d73 6579 2c45 6c61 696e 6500 0000 0000 0000 0000 0000 0042 5553 0050 5359 0000 0000 6b00 0000 5857 6861 7274 6f6e 2c54 6f6d 0000 0000 0000 0000 0000 0000 0000 4255 5300 5053 5900 0000 0075 0000 0062 4261 6b65 722c 4e6f 726d 6100 0000 0000 0000 0000 0000 0000 0042 494f 0043 5349 0000 0000 2700 0000 19 

any appreciated! thank you!

you have problem here

fscanf(f1, "%s", attr); fscanf(f1, "%1s", type); fscanf(f1, "%d", &numbytes); 

you must sure read correct values, need check return value of fscanf(), , can fit in single format string this

if (fscanf(f1, "%s%1s%d", attr, type, &numbyts) != 3)     /* ... there problem data, not continue ... */ 

also, give length modifiers every "%s" specifier prevent buffer overflow.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -