reading from structs in a binary file

This is a discussion on reading from structs in a binary file within the C Programming forums, part of the General Programming Boards category; Hi, I created a program that takes in input from the user and stores it in a struct and then ...

  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
    zen
    zen is offline
    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
    zen
    zen is offline
    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, 09: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, 06:51 PM
  5. Reading data from a binary file
    By John22 in forum C Programming
    Replies: 7
    Last Post: 12-06-2002, 01:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21