![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
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 |
|
#3
|
|||
|
|||
|
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 |
|
#4
|
|||
|
|||
|
man fgets.
|
|||
| Google The UNIX and Linux Forums |