I have some binary file that i write struct objects to(of one defined type). I want to be able to read particular (ith) "struct block" from a binary file to a struct and display it. The only idea that comes to my mind is to create an array of structures containning all of them so that i could have an access to the ordinary one but it doesn't seem to be an efficient way. I would appreciate if one could help me with this problem :)
Asked
Active
Viewed 423 times
1
-
2If you've written code that can successfully write structs to a file, then you have the ability to at least make an effort at this. We're not a code writing service. – Carey Gregory May 16 '15 at 23:07
-
Since you know the size of each structure it should be very easy. – Some programmer dude May 16 '15 at 23:10
-
1When you are writing your example to post, consider a file offset of `(ith - 1) * sizeof (struct yourstruct)`. – David C. Rankin May 16 '15 at 23:19
-
Carey Gregory: Where did i ask to write code for me? I just ask for explainning procedure. I don't know how to use fread function for this purpose since there is no pointer specifier to refer to particular position in file. – paul867 May 16 '15 at 23:25
1 Answers
3
I am new to C but think I can help with this. Is this the kind of thing you were looking to do:
#include<stdio.h>
#include<stdlib.h>
//just a struct for purposes of demonstration
struct my_struct{
int prop1;
int prop2;
};
//writes structs to filename.dat
void writeStruct(int property){
FILE *file_pointer;
file_pointer = fopen("filename.dat","ab");
//define and assign variables to a quick dummy struct
struct my_struct this_struct;
this_struct.prop1=property;
this_struct.prop2=property*2;
//write struct to file
fwrite(&this_struct, sizeof(this_struct), 1, file_pointer);
fclose(file_pointer);
}
//returns the nth struct stored in "filename.dat"
struct my_struct getNthStruct(long int n){
FILE *file_pointer;
file_pointer = fopen("filename.dat","rb");
//will be the struct we retrieve from the file
struct my_struct nth_struct;
//set read position of file to nth struct instance in file
fseek(file_pointer, n*sizeof(struct my_struct), SEEK_SET);
//copy specified struct instance to the 'nth_struct' variable
fread(&nth_struct, sizeof(struct my_struct), 1, file_pointer);
return nth_struct;
}
int main(){
//write a bunch of structs to a file
writeStruct(1);
writeStruct(2);
writeStruct(3);
writeStruct(4);
writeStruct(5);
//get nth struct (2 is third struct, in this case)
struct my_struct nth_struct;
nth_struct=getNthStruct(2);
printf("nth_struct.prop1=%d, nth_struct.prop2=%d\n",
nth_struct.prop1, //outputs 3
nth_struct.prop2); //outputs 6
return 0;
}
I intentionally didn't check for obvious errors (FILE pointers returning NULL
, length of files, etc) for brevity and isolating the core concept.
Feedback is welcome.

Community
- 1
- 1

user1717828
- 7,122
- 8
- 34
- 59
-
Except for the absence of error handling, which you explicitly mention, what you've got works nicely for simple structures with a fixed size and no pointers. Life gets trickier if you have to deal with variable size structures, or the structures contain pointers instead of, say, fixed size arrays. Structures using a FAM (flexible array member) are also problematic because they too are not a fixed size. Such issues can be dealt with; it requires a _lot_ more code to do so. – Jonathan Leffler May 17 '15 at 15:23
-
1One change worth thinking about is combining declaration with initialization. For example, you could replace: `FILE *file_pointer; file_pointer = fopen("filename.dat","rb");` (2 statements) with `FILE *file_pointer = fopen("filename.dat","rb");` (1 statement). That works in C89. Similarly, you could replace `struct my_struct this_struct; this_struct.prop1=property; this_struct.prop2=property*2;` (3 statements) with `struct my_struct this_struct = { property, property*2 };` (C89) or even `struct my_struct this_struct = { .prop1 = property, .prop2 = property*2 };` (C99 designated initializers). – Jonathan Leffler May 17 '15 at 15:29