what am i missing here??

This is a discussion on what am i missing here?? within the C Programming forums, part of the General Programming Boards category; here's a code i wrote to find out how non-blocking read works... Code: #include<stdio.h> #include<stdlib.h> #include<fcntl.h> #include<errno.h> int main() { ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    23

    what am i missing here??

    here's a code i wrote to find out how non-blocking read works...


    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<fcntl.h>
    #include<errno.h>
    
    int main()
    {
    	int fd;
    	int n;
    	char buf[1024];
    	char ch;
    
    	fd = open("/dev/tty", O_RDWR | O_NONBLOCK);
    	if (fd == -1) {
    		perror("open");
    		exit(EXIT_FAILURE);
    	}
    
    	while ((n = read(fd, buf, 1024)) != 0) {
    		if (n == -1) {
    			if (errno == EAGAIN) {
    				printf("no data\n", n);
    				sleep(2);
    			}
    		}
    		else {
    			buf[n] = '\0';
    			printf("read %s\n", buf);
    			break;
    		}
    	}
    	exit(0);
    }
    the thing is that i cannot really determine how this read works. just typing on the terminal doesn't make the read return and it keeps on returning -1 with errno = EAGAIN.
    the data from the terminal is read only on hitting enter. so why is that needed. how can i get the tty to work like a normal file with read.

    thanks

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,639
    See here....looks like you need to set the device to non-canonical mode.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    Rochester, NY
    Posts
    196
    Damn, my friend had the same exact problem with nb reads quite similar to this (although not from a term).

    Blahhh, can't remember for the life of me what the issue was...

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    23
    thanks.. i know about non canonical input. the thing is that if this read was reading from an open file it would have returned after reading data from it, without the need of any key press. it's the working of read on terminals and their link to the return key. i can't find the exact definition of how a read interprets the carriage return.

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    23
    why wont anybody answer??? why???

  6. #6
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    Code:
    #include<stdlib.h>
    #include<fcntl.h>
    #include<errno.h>
    
    int main()
    {
    	int fd;
    	int n;
    	char buf[1024];
    	char ch;
    
    	fd = open("/dev/tty", O_RDWR | O_NONBLOCK);
    	if (fd == -1) {
    		perror("open");
    		exit(EXIT_FAILURE);
    	}
    
    	while ((n = read(fd, buf, 1024)) != 0) {
    		if (n == -1) {
    			if (errno == EAGAIN) {
    				printf("no data\n", n); // you forgot something...
    				sleep(2);
    			}
    		}
    		else {
    			buf[n] = '\0';
    			printf("read %s\n", buf);
    			break;
    		}
    	}
    	exit(0);
    }
    what you want is to print this:

    Code:
    no data
    ?

    if yes delete the "n"
    Last edited by brack; 09-03-2010 at 08:37 AM.

  7. #7
    Registered User
    Join Date
    Aug 2010
    Posts
    23

    corrected -

    here's a code i wrote to find out how non-blocking read works...


    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<fcntl.h>
    #include<errno.h>
    
    int main()
    {
    	int fd;
    	int n;
    	char buf[1024];
    	char ch;
    
    	fd = open("/dev/tty", O_RDWR | O_NONBLOCK);
    	if (fd == -1) {
    		perror("open");
    		exit(EXIT_FAILURE);
    	}
    
    	while ((n = read(fd, buf, 1024)) != 0) {
    		if (n == -1) {
    			if (errno == EAGAIN) {
    				printf("no data\n");
    				sleep(2);
    			}
    		}
    		else {
    			buf[n] = '\0';
    			printf("read %s\n", buf);
    			break;
    		}
    	}
    	exit(0);
    }
    the thing is that i cannot really determine how this read works. just typing on the terminal doesn't make the read return and it keeps on returning -1 with errno = EAGAIN.
    the data from the terminal is read only on hitting enter. so why is that needed. how can i get the tty to work like a normal file with read.

    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what am I missing? (Program won't compile)
    By steals10304 in forum C Programming
    Replies: 3
    Last Post: 08-25-2009, 03:01 PM
  2. LDAP Query
    By Travoiz in forum C++ Programming
    Replies: 0
    Last Post: 08-13-2009, 02:58 PM
  3. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 02:49 PM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 03:49 AM

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