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
how do collect shell output in a C program damn_bkb UNIX for Advanced & Expert Users 5 05-25-2009 11:23 PM
C program Output arunviswanath High Level Programming 3 07-05-2007 02:16 AM
how can I use the stream output in other program umen UNIX for Dummies Questions & Answers 1 02-02-2006 07:55 AM
Capturing output from C++ program GMMike UNIX for Dummies Questions & Answers 3 03-08-2005 09:04 AM
Capturing cli Program output Christopher High Level Programming 1 12-06-2001 11:08 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-21-2008
bishweshwar bishweshwar is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 56
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.
  #2 (permalink)  
Old 01-21-2008
bishweshwar bishweshwar is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 56
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 (permalink)  
Old 01-21-2008
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,958
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 (permalink)  
Old 01-21-2008
ramen_noodle ramen_noodle is offline Forum Advisor  
Registered User
  
 

Join Date: Dec 2007
Location: Virginia, USA.
Posts: 251
man fgets.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:07 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0