Results 1 to 2 of 2

Thread: read problem in c

  1. #1
    Join Date
    May 2013
    Beans
    27

    read problem in c

    hi all;

    i have run this code and the first read and write work correctly. but and the second one is missed.
    the output log is:


    freez@JALALI:~$ ./a
    salam8a
    okok2freez@JALALI:~$
    Code:
     
    
    
    • #include <stdio.h>
    • int main()
    • {
    • write(1,"salam",5);
    • char a[2];
    • char b[5];
    • read(0,a,2);
    • if(a[0]=='8')
    • {write(1,"ok",2);
    • read(0,b,5);
    • write(1,"ok2",3);
    • }
    • return 0;
    • }

  2. #2
    Join Date
    Aug 2010
    Location
    Lancs, United Kingdom
    Beans
    1,119
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: read problem in c

    Your first read reads the 2 characters '8' and 'a'. There is a 3rd character waiting to be read, which is '\n'. Your second read reads this '\n', so your program never gives you chance to enter a second line of text.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •