Thread: How do I pass an array of structures to the *nix read() function.

  1. #1
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329

    How do I pass an array of structures to the *nix read() function.

    I'm trying to pass an array of structures to the *nix read() function. Here is the code...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <errno.h>
    
    
    struct bbs {
      char *s;
      int i;
    };
    
    struct bbs user[1];
    
    int main(void)
    {
    
      user[0].s = "chad\n";
      user[0].i = 1;
    
      /*user[1].s = "was\n";
      user[1].i = 1;
    
      user[2].s = "here\n";
      user[2].i = 1;
      */
    
    
      ssize_t n;
      int fd;
    
      if ((fd = open("/home/cdalten/oakland/ho", O_RDWR)) < 0) {
        perror("Can't open file\n");
        exit(EXIT_FAILURE);
      }
    
      write(fd, &user, sizeof(struct bbs));
    
      while((n = read(fd, &user, sizeof(struct bbs))) > 0 ) {
        printf("The string is: %s\n", user[1].s);
      }
    
      close(fd);
     
      exit(EXIT_SUCCESS);
    }
    And here is what I get when I compile and run the code.
    [cdalten@localhost oakland]$ gcc -g -Wall wr.c -o wr
    [cdalten@localhost oakland]$ ./wr
    The string is: (null)
    The string is: (null)
    [cdalten@localhost oakland]$

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    1) You haven't allocated any memory to the bbs objects.
    2) Even if you had, you would need to pass the address of the memory (not the structure) to the read routine (otherwise, you'd just be overwriting the pointer).
    3) When you do finally allocate some memory, just remember to pass the size of the buffer (not the structure) as the third parameter of the read function.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Writing out a structure that contains pointer values just writes meaningless data to the disk. The pointers will not be useful (or even necessarily valid) in another invocation of the program. You need to write out the data which is pointed to, not the pointer value itself.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How To pass 2 dimensional array of strings to a function
    By chottachatri in forum C Programming
    Replies: 15
    Last Post: 01-25-2008, 02:20 PM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. Replies: 6
    Last Post: 02-15-2005, 11:20 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM