Thread: reading from structs in a binary file

  1. #1
    Unregistered
    Guest

    reading from structs in a binary file

    Hi, I created a program that takes in input from the user and stores it in a struct and then takes that struct and stores it into a binary file. Now what I'd like to do is read a particular piece of information from those records, like the of age of a person, so that I can compare it to the ages within other records. How would I go about taking out those particular bits of info?

    Say I have a this struct erecc[x].age stored in that binary, how would I get it out?

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    You could read a whole struct from file into a temporary struct in one go and then access the value through the temporary struct.
    zen

  3. #3
    Unregistered
    Guest
    How would I go about doing that? I see where you're going with this but I'm unfamiliar with the commands.

  4. #4
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Something like -

    struct something s;
    fread(&s,sizeof(struct something),1,fileptr);

    then you'd access elements of the struct normally.
    zen

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    34
    Example:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define DATA_FILE  "data~.txt"
    
    struct DataRec
    {
    	char Name[31];
    	unsigned int Age;
    };
    
    static void WriteData();
    static void ReadData();
    
    int main(int argc, char* argv[])
    {
    	/* CREATE SOME TEST DATA */
    	WriteData();
    
    	/* READ THE TEST DATA BACK FROM THE FILE */
    	ReadData();
    
    	return 0;
    }
    
    static void
    WriteData()
    {
    	FILE *fp;
    	struct DataRec MyRec;
    
    	if ((fp = fopen(DATA_FILE, "w")) != NULL)
    	{
    		strcpy(MyRec.Name, "John Public");
    		MyRec.Age = 25;
    
    		fwrite(&MyRec, sizeof(MyRec), 1, fp);
    
    		strcpy(MyRec.Name, "Mike Miller");
    		MyRec.Age = 30;
    
    		fwrite(&MyRec, sizeof(MyRec), 1, fp);
    
    		fclose(fp);
    	}
    }
    
    static void
    ReadData()
    {
    	FILE *fp;
    	struct DataRec Rec1;
    	struct DataRec Rec2;
    
    	if ((fp = fopen(DATA_FILE, "r")) != NULL)
    	{
    		/* read the first structure but don't display it */
    		fread(&Rec1, sizeof(DataRec), 1, fp);
    		
    		/* read the second structure and display it */
    		fread(&Rec2, sizeof(DataRec), 1, fp);
    
    		printf("Name: %s  Age: %d\n", Rec1.Name, Rec1.Age);
    		printf("Name: %s  Age: %d\n", Rec2.Name, Rec2.Age);
    
    		if (Rec1.Age == Rec2.Age)
    			printf("Ages are the same\n");
    		else
    			printf("Ages are different\n");
    		
    
    		fclose(fp);
    	}
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  2. Reading in a binary file from offset into another file
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 05-24-2006, 03:01 AM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  4. Problem reading a delimited file into a binary tree
    By neolyn in forum C++ Programming
    Replies: 10
    Last Post: 12-09-2004, 07:51 PM
  5. Reading data from a binary file
    By John22 in forum C Programming
    Replies: 7
    Last Post: 12-06-2002, 02:00 PM