The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
C program Output arunviswanath High Level Programming 3 07-04-2007 10:16 PM
how do collect shell output in a C program damn_bkb UNIX for Advanced & Expert Users 2 12-31-2006 10:36 AM
how can I use the stream output in other program umen UNIX for Dummies Questions & Answers 1 02-02-2006 04:55 AM
Capturing output from C++ program GMMike UNIX for Dummies Questions & Answers 3 03-08-2005 06:04 AM
Capturing cli Program output Christopher High Level Programming 1 12-06-2001 08:08 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 01-21-2008
Registered User
 

Join Date: Sep 2006
Posts: 55
ambiguity in program output

1 #include <fcntl.h>
2
3 main(int argc, char *argv[])
4 {
5 char buf[1];
6 int fd, count = 0;
7
8 if (argc > 1)
9 fd = open(argv[1], O_RDONLY);
10 else
11 fd = 0; /* Use standard input */
12
13 while (read(fd, buf, 1) > 0) {
14 if (count < 5) write(1, buf, 1);
15 if (buf[0] == '\n') count++;
16 }
17 }

Here buf can store only one character.
But if i run it as:-

"prog_name /etc/passwd" how does it give first five lines.
Reply With Quote
Forum Sponsor
  #2  
Old 01-21-2008
Registered User
 

Join Date: Sep 2006
Posts: 55
Sorry for bothering anyone if read it.
Got the answer.

read() function reads an entire line from input, i was thinking it of as a character.

Thanks
bishweshwar
Reply With Quote
  #3  
Old 01-21-2008
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,610
Please use CODE tags ,

its quite difficult to read through the code without them.

It seems that the code is pasted from editor with line numbers set
Reply With Quote
  #4  
Old 01-21-2008
Registered User
 

Join Date: Dec 2007
Location: Virginia, USA.
Posts: 250
man fgets.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 12:52 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0