help with read()

This is a discussion on help with read() within the C Programming forums, part of the General Programming Boards category; Hi all, I'm trying to read a misc device /dev/pi When I do "cat /dev/pi" I get my desired output. ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    2

    help with read()

    Hi all, I'm trying to read a misc device /dev/pi

    When I do "cat /dev/pi" I get my desired output. However, I'm trying to test with the following code and running into problems.

    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    
    
    char buffer[10];
    
    int main()
    {
    
      int test = open("/dev/pi", O_RDWR);
      if (test < 0)
        printf ("Couldn't open file\n");
      else
        printf("File Opened Successfully\n");
    
    
      int test2 = read(test,*buffer,sizeof(buffer));
    
      printf("The fd is: %i\n", test);
    
      printf("Output from read function: %i\n", test2);
    
      printf("Pi: %s", buffer);
    
    }
    Seems to open the file successfully but it doesn't read the file correctly. This is the output:

    File Opened Successfully
    The fd is: 3
    Output from read function: -1
    Pi:

    Any help?

  2. #2
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    int test2 = read(test,*buffer,sizeof(buffer));
    I believe read requires the address of the buffer. Since you defined it as a char array, its address would be simply buffer.

    Code:
    int test2 = read(test,buffer,sizeof(buffer));

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read Causes Freezing?
    By Masna in forum C Programming
    Replies: 5
    Last Post: 07-18-2008, 05:31 PM
  2. Read() problems
    By yay4rei in forum C Programming
    Replies: 2
    Last Post: 07-21-2005, 11:47 AM
  3. How can I know the actual bytes read in a file read
    By pliang in forum C++ Programming
    Replies: 1
    Last Post: 06-08-2005, 05:23 PM
  4. What Would You Use To Read User Input?
    By djwicks in forum C Programming
    Replies: 11
    Last Post: 04-05-2005, 04:32 PM
  5. Read Array pro!!Plz help!!
    By Supra in forum C Programming
    Replies: 2
    Last Post: 03-04-2002, 03:49 PM

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