Does anyone know how to read the contents of a binary file into a static struct array, that can then be searched like a normal array?

An array of structures with what members?

If you do not know the format of the binary file about the best you can do is read it into an array of char.

Well, yeah. It's basically a binary file which acts as a database of users. So it will contain usernames and passwords. My current code goes like this:

static struct account users[] = {
    {"root", "0426"}
};

And the program matches the user "root", to the password "0426", however I would rather do this with binary files.

So the struct will need two members, something like:

struct mystruct {
  char name[20];
  char passwrd[10];
}

Before main(), then in main(), create the struct itself:

struct mystruct mine[20];

and now you have an array of twenty records. Pass mine around like any other array, to your other functions, and remember, at that point, mine is a pointer, not a full array (but you can modify it's members that way).

Edited 5 Years Ago by Adak: n/a

fread() to read binary records
fwrite() to write binary records

Want an example of fread()?

Back in a sec.

I know how to read and write binary records as my program does this already, I am just confused as to how to read it directly into an array which can be searched.

Ah! OK, well, obviously in a while loop:

i=0;
while(1) {
   if(fread(&myArrayName[i++], sizeOfYourRecord, 1, FILE *pointer) < 2) 
     break;  //fread returns the number of record members it stored)
}
ÜÜÜÜÜÜÜ
 ÝfreadÞ   Reads data from a stream.
 ßßßßßßß

 Syntax:
   size_t fread(void *ptr, size_t size, size_t n, FILE *stream);

 Prototype in:
 stdio.h

 Remarks:
fread reads n items of data, each of length
size bytes, from the given input stream into a
block pointed to by ptr.

The total number of bytes read is (n x size).

 Return Value:
On successful completion, fread returns the
number of items (not bytes) actually read.

It returns a short count (possibly 0) on
end-of-file or error.

 Portability:
fread is available on all UNIX systems and is
defined in ANSI C.

 See Also:
  fopen    fwrite    printf    read

 Example:
 #include <string.h>
 #include <stdio.h>

 int main(void)
 {
    FILE *stream;
    char msg[] = "this is a test";
    char buf[20];

    if ((stream = fopen("DUMMY.FIL", "w+"))
        == NULL)
    {
       fprintf(stderr, "Cannot open output file.\n");
       return 1;
    }

    /* write some data to the file */
    fwrite(msg, strlen(msg)+1, 1, stream);

    /* seek to the beginning of the file */
    fseek(stream, SEEK_SET, 0);

    /* read the data and display it */
    fread(buf, strlen(msg)+1, 1, stream);
    printf("%s\n", buf);

    fclose(stream);
    return 0;
 }

Edited 5 Years Ago by Adak: n/a

This article has been dead for over six months. Start a new discussion instead.