Writing an entire struct to a file with fwrite() in UNIX

This is a discussion on Writing an entire struct to a file with fwrite() in UNIX within the C Programming forums, part of the General Programming Boards category; When I try to write an entire struct to a file and I read the file afterwards (on mac os ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    44

    Writing an entire struct to a file with fwrite() in UNIX

    When I try to write an entire struct to a file and I read the file afterwards (on mac os X), I don't get the actual values I inputted, but some hex values. At first I thought this might be possible because it should be in binary mode (although Unix doesn't separate binary and text mode) so I tried printing the file I just wrote. But I do not get any values when I do this.

    Is it not possible to do what I am trying to do unless you work with Windows, or am I doing something wrong?

    (Note that it has nothing to do with the functions getLine() and getche(). These are correct alternatives for gets() and windows _getche() ).

    writing struct values to a file:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    #include "/Users/jeroen/Documents/uhasselt/C/safegets unixgetch/unixgetch.h"
    #include "/Users/jeroen/Documents/uhasselt/C/safegets unixgetch/safe_gets.h"
    
    
    int main(int argc, char *argv[])
    {
    	struct agentInfo
    	{
    		int id;
    		float height;
    		char *name;
    	} agent;
    
    	char *numstr;
    	char yesno;
    	FILE *fptr;
    	fptr = fopen("log.txt", "w");
    
    	do
    	{
    		puts("Gief agent ID");
    		numstr = getLine();
    		agent.id = atoi(numstr);
    		printf("%i", agent.id);
    		puts("Gief name");
    		agent.name = getLine();
    		puts("Gief height");
    		numstr = getLine();
    		agent.height = atof(numstr);
    		fwrite(&agent, sizeof(agent), 1, fptr);
    		puts("Add another agent?");
    	} while ((yesno = getche()) == 'y');
    
    	fclose(fptr);
    	return 0;
    }
    reading these values:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    #include "/Users/jeroen/Documents/uhasselt/C/safegets unixgetch/unixgetch.h"
    #include "/Users/jeroen/Documents/uhasselt/C/safegets unixgetch/safe_gets.h"
    
    
    int main(int argc, char *argv[])
    {
    	struct agentInfo
    	{
    		int id;
    		float height;
    		char *name;
    	} agent;
    
    	FILE *fptr;
    	fptr = fopen("log.txt", "rb");
    
    	while (fread(&agent, sizeof(agent), 1, fptr) == 1)
    	{
    		printf("\nName: %s\n", agent.name);
    	}
    
    	fclose(fptr);
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    You don't open the file you're writing for binary, which might matter.

    Furthermore, this method is unreliable, it depends on the endianness and internal representation of numbers of the processor. I believe Macs are based on x86, which is little endian, so that shouldn't matter in this case though.

    Also, don't use full paths for #include.

    Also, what input values did you use and what was the file's content?

    Also, sorry for all those furthermores and also's.
    Last edited by EVOEx; 03-16-2010 at 09:28 AM.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Ahh, never mind my last post :P. You're storing a POINTER to a string in the structure, not the actual string. So what you save in the file is the pointer's value, not the actual text. Of course, the pointer value is completely useless now as it's a dangling pointer after reading.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    44
    You are correct that was wrong :-) thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems installing Mingw5.1.4
    By BlackOps in forum C Programming
    Replies: 2
    Last Post: 07-26-2009, 04:28 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Bi-Directional Linked Lists
    By Thantos in forum C Programming
    Replies: 6
    Last Post: 12-11-2003, 10:24 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

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