Thread: read function

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    41

    read function

    read(int d, void *buf, size_t nbytes);


    How can i use the above function to read a file and store the content of the file in char variable ??? I have got the buffer size already. But i dont know how to use the function to read the file into a variable.

    Code:
    	if(-1==(fd=open(argv[1], O_RDONLY,0666))) 
    	{
    		perror("opening");
    		exit(1);
    	}	
                fstat(fd, &buf);
                len = buf.st_size;

  2. #2
    Watch for flying houses. Nessarose's Avatar
    Join Date
    Sep 2004
    Posts
    46
    Something like this would read the first BUFSIZ bytes in the file and store it into array:

    Code:
    #include <stdio.h>
    #include <errno.h>
    #include <unistd.h>
    #include <fcntl.h>
    
    int main(int argc, char *argv[]) 
    {
        char array[BUFSIZ];
        int fd = open(argv[1], O_RDONLY);
        if (fd == -1) {
           perror("open");
           exit(errno);
        }
        read(fd, array, BUFSIZ);
        printf("%s", array);
        if (close(fd) == -1) {
            perror("close");
            exit(errno);
        }
        return 0;
    }

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why use open? Why not make it easy on yourself and just use fopen, and fread or fgets? or is there some reason you can't use FILE streams?

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Why use a FILE stream when using a low-level function would be just as easy and would be faster? I don't really see any advantage of FILE streams over low-level I/O in this (aside from a reduced number of header files).
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <unistd.h>
    
    int main(void)
    {
      char buf[BUFSIZ], *file = NULL;
      unsigned long size = 0;
      int n_read;
      int fd;
    
      if((fd = open("somefile", O_RDONLY)) == -1)
      {
        puts("Couldn't open somefile for reading!");
        exit(EXIT_FAILURE);
      }
    
      while((n_read = read(fd, buf, sizeof(buf))) != -1)
      {
        if(!(file = realloc(file, size+n_read)))
        {
          puts("Memory allocation error!");
          exit(EXIT_FAILURE);
        }
        memcpy(file+size, buf, n_read);
        size += n_read;
    
        if(n_read < sizeof(buf))
          break;
      }
      close(fd);
    
      printf("Bytes read: %lu\n", size);
      puts("\nFile dump:");
      puts(file);
    
      return EXIT_SUCCESS;
    }
    Last edited by itsme86; 09-18-2004 at 11:50 PM.
    If you understand what you're doing, you're not learning anything.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by itsme86
    Why use a FILE stream when using a low-level function would be just as easy and would be faster? I don't really see any advantage of FILE streams over low-level I/O in this (aside from a reduced number of header files).
    There are more stream based functions than there are non-stream based functions. That's why. Because you've got a fuller library of functions at your disposal.

    As to your code:
    1) If realloc fails, you're screwed:
    Code:
    if(!(file = realloc(file, size+n_read)))
    2) You never free what you've allocated.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Here's an even simpler design:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <unistd.h>
    
    int main(void)
    {
      char *file = NULL;
      struct stat statbuf;
      int fd;
    
      if((fd = open("somefile", O_RDONLY)) == -1)
      {
        puts("Couldn't open somefile for reading!");
        exit(EXIT_FAILURE);
      }
      if(fstat(fd, &statbuf) == -1)
      {
        puts("Couldn't stat somefile!");
        exit(EXIT_FAILURE);
      }
      if(!(file = malloc(statbuf.st_size)))
      {
        puts("Memory allocation error!");
        exit(EXIT_FAILURE);
      }
      if(read(fd, file, statbuf.st_size) == -1)
      {
        puts("Error reading from file!");
        exit(EXIT_FAILURE);
      }
      close(fd);
    
    
      printf("Bytes read: %lu\n", statbuf.st_size);
      puts("\nFile dump:");
      puts(file);
    
      return EXIT_SUCCESS;
    }
    If you understand what you're doing, you're not learning anything.

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by quzah
    There are more stream based functions than there are non-stream based functions. That's why. Because you've got a fuller library of functions at your disposal.

    As to your code:
    1) If realloc fails, you're screwed:
    Code:
    if(!(file = realloc(file, size+n_read)))
    2) You never free what you've allocated.

    Quzah.
    I realize this, but you don't need any of them. You only need to open file, read from it once, and then close it. Don't need to worry about newlines or anything else in the content of the file.

    EDIT: You're right, I unintentionally didn't free() the memory. Thanks for pointing that out.
    Last edited by itsme86; 09-19-2004 at 12:04 AM.
    If you understand what you're doing, you're not learning anything.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I was just suggesting alternative ways of doing things. Does the stack count as a buffer?
    Code:
    #include <stdio.h>
    void dread( FILE *fp )
    {
        int c = fgetc( fp );
        if( c != EOF )
        {
            putc( c );
            dread( fp );
        }
    }
    
    int main( void )
    {
        FILE *fp = fopen( "somefile", "r" );
        if( fp )
        {
            dread( fp );
            fclose( fp );
        }
        return 0;
    }


    Don't need to worry about newlines or anything else in the content of the file.
    Of course, we're both assuming this is a text file full of printable characters too...

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Read In From Many Files In One Function
    By djwicks in forum C Programming
    Replies: 12
    Last Post: 03-24-2005, 07:39 AM
  4. Replies: 5
    Last Post: 02-08-2003, 07:42 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM