LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-24-2003, 09:23 AM   #1
dummyagain
Member
 
Registered: Sep 2003
Posts: 74

Rep: Reputation: 15
read and pipe function


How to use the read(fd, ?/, ??) to read the line in the other file?

and also what's the use of pipe() and can u suggest some examples for showing the usage?

Thanks
 
Old 09-24-2003, 10:26 AM   #2
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 33
READ ::-

#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);
read() attempts to read up to count bytes from file descriptor fd into the buffer starting at buf.
Code:
#include<stdio.h>
#include<unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
main()
{
        int fd=open("file",O_RDONLY);
        char BUFF[50];
        while(read(fd,BUFF,50)!=0)
                printf("%s",BUFF);
}
 
Old 09-24-2003, 10:30 AM   #3
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 33
PIPES :-
Pipes are mainly used for Inter Process Communication ....
Code:
#include <stdio.h>
#include <unistd.h>
main()
{
        int  pipefd[2];
        int pid;
        char child_buff[100] = "This is a buffer :: I am doing IPC";
        char parent_buff[100];
        if( pipe(pipefd) < 0)
        {
                printf("Error in creating pipe!\n");
                return;
        }
        if( (pid = fork()) < 0)
        {
                printf("Error in fork\n");
                return;
        }
        if(pid == 0)
        {
                if( write(pipefd[1], child_buff, 50) < 0)
                {
                        printf("write error!\n");
                        return;
                }
        }
        else
        {
                read(pipefd[0], parent_buff, 50);
                printf("%s\n", parent_buff);
        }
}
You can do "man 2 pipe" and "man 2 read" to get more information about these two functions...
 
Old 09-24-2003, 10:48 AM   #4
dummyagain
Member
 
Registered: Sep 2003
Posts: 74

Original Poster
Rep: Reputation: 15
What's the buf in read() ? How do we determine its value?
 
Old 09-24-2003, 10:50 AM   #5
dummyagain
Member
 
Registered: Sep 2003
Posts: 74

Original Poster
Rep: Reputation: 15
My concept to the pipe() is still not clear, can anybody show me an example, say to access a folder and check the no of file, can the pipe() do this?
 
Old 09-24-2003, 11:08 AM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally posted by dummyagain
What's the buf in read() ? How do we determine its value?
It's a string, or, in C an array of char's.
After it's declared ( char buf[50]; ) there memory set aside to contain 50 char's. After this declaration the bytes (char's) are there for you to use, but only contain garbage, i.e. it contains undefined bytes. But they get overwritten by the read() function with the contents from the file read() is reading. So, after read(), the bytes now have a meaning (text from the file).

buf points to the first char (byte) of the array, which is buf[0]. When reading text, and you want to handle buf like a string (e.g. print it with puts() / printf(), or compare it with another string with strncmp() ), then you can only store 49 char's because the last needs to be the special null-character ( '\0' ) for the string handling to know where it ends.

Here's a larger example. Maybe it helps:
Code:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

/* Reads the first line of a file and prints it.
 * The name of the file must be given as the 
 * only argument on the command line.
 * The maximum length of the line read and printed
 * is defined below (MAXLEN).
 */

#define MAXLEN 100

int main(int argc, char *argv[])
{
     int fd;
     int count, i;
     char line[MAXLEN + 1];

     if (argc != 2) {
	  fprintf(stderr, "\nUsage: %s <filename>\n\n", argv[0]);
	  return 1;
     }

     fd = open(argv[1], O_RDONLY);
     if (fd < 0) {
	  perror("Error opening file");
	  return 1;
     }

     count = read(fd, line, MAXLEN);
     if (count < 0) {
	  perror("Error reading file");
	  return 1;
     }

     /* Find the end of the line and make it the end of the
      * string.  (it's possible we read more than 1 line)
      * Another option would be to read() one byte at a time
      * using read(fd, line, 1), and then check for this 
      * after each byte read.
      */
     for (i = 0; i < count ; ++i) {
	  if (line[i] == '\n') {
	       line[i] = '\0';
	       break;
	  }
     }

     /* If no '\n' found, then the line was longer than MAXLEN
      * And the line will be truncated. In that case we have to
      * make sure the string is terminated at the end.
      */
     line[MAXLEN] = '\0';

     /* Print the line */
     printf("%s\n", line);
     return 0;
}

Last edited by Hko; 09-24-2003 at 11:09 AM.
 
Old 09-24-2003, 11:21 AM   #7
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally posted by dummyagain
My concept to the pipe() is still not clear, can anybody show me an example, say to access a folder and check the no of file, can the pipe() do this?
No, pipe() cannot do that. Use stat() and/or readdir() for that.
But don't bother until you understand C better, and understand things like "char buf[50]" and struct's well.

A pipe is use to connect two processes (say "programs"), where one process read output from the other. pipe() is used mostly when your program starts another program, or creates another process running the same program, and you want to pass data from your program to the other.

These kind of functions (popen(), pipe(), ...) are used by the shell (bash) for example to make things possible like this:

$ zcat somefile.txt.gz | sort | uniq | fgrep someword

(this example doesn't really make sense, just to illustrate pipes)

I don't mean to offend...but if you don't what pipes are and what they do, and don't understand C reasonably well, I would not bother trying to write programs using pipe() and read(). These aren't the most easy functions to start with.

Hope I could help a bit anyway.

Last edited by Hko; 09-24-2003 at 11:27 AM.
 
Old 09-24-2003, 11:26 AM   #8
dummyagain
Member
 
Registered: Sep 2003
Posts: 74

Original Poster
Rep: Reputation: 15
Yes, I am just a beginner in using C (I used to use C++, but I need to learn it due to the project)...

I just wonder if I can use the pipe() to do that

One of the side of the pipe is for standard input while other side is for standard output. If I put the folder in the input and make it count the no of file in the folder and then output it to the output of other side (The standard output). The two sides of the pipe will be used by using folk() to produce both parent and child....

can it work?
 
Old 09-24-2003, 11:47 AM   #9
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 33
Yes it sure will work .... All that you have to do is pass the no. of files as the input through child_buff in the program I have already
posted....

There the string "This is a buffer :: I am doing IPC" was being transmitted....

P.S :- I suppose you know the functions required to count no. of files i.e, scandir()....
 
Old 09-24-2003, 03:21 PM   #10
rmurberger
LQ Newbie
 
Registered: Sep 2003
Posts: 3

Rep: Reputation: 0
Smile Pipe

How do you type the pipe symbol? I know I read it somewhere but now that I need it I cant find it. A REAL Newbie question!!
 
Old 09-24-2003, 03:46 PM   #11
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Re: Pipe

Quote:
Originally posted by rmurberger
How do you type the pipe symbol?
By pushing gently on the button on your keyboard that says: |
There are about more than 100 (!) buttons on your keyboard, so you may have to have a look at all of them to find it. Sorry, there's no easier way, no hacker tricks or shortcuts available here.

If it is on a button together with another symbol and the | is the top one, it gets a little complex:

Before you gently press the button, you have to press and hold another button on the keyboard that says: "Shift". Fortunately there are two of them and it does not matter which one you use, and they usually are slightly larger than the most buttons, so you should be able to find them relatively easily. After you have pressed the button saying | while holding one of the shift buttons, you can also release the shift button.

Good luck!

Quote:
A REAL Newbie question!!
More or less...

Last edited by Hko; 09-24-2003 at 03:55 PM.
 
Old 09-24-2003, 03:57 PM   #12
rmurberger
LQ Newbie
 
Registered: Sep 2003
Posts: 3

Rep: Reputation: 0
Pipe

Thanks for for your sarcastic response Hko. I have 3 keyboards and none have a pipe key. I may new to Linux but I'm hardly new to keyboards and not stupid either. If I had a pipe symbol on my keyboard I wouldnt be asking such a "stupid" question. These things and sarcastic responses from people like you certainly dont encourage windows users like me to make the transition to Linux.
 
Old 09-24-2003, 05:47 PM   #13
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
OK, you're right. I should not have done that.
I could not resist the temptation, I guess.
It didn't come to my mind there would be PC-keyboards without the |.
This was stupid of me, because obviously there could be keyboards for languages-other-than-english (or dutch for that matter) that do not have hey for |. What kind of keyboard are you using?

Actually, I do not know a simple answer to your question.

I apologize

Quote:
These things and sarcastic responses from people like you certainly dont encourage windows users like me to make the transition to Linux.
True.
 
Old 09-24-2003, 10:36 PM   #14
shellcode
Member
 
Registered: May 2003
Location: Beverly Hills
Distribution: Slackware, Gentoo
Posts: 350

Rep: Reputation: 32
if you want to learn about pipes and other types of UNIX IPC check out:
http://www.ecst.csuchico.edu/~beej/guide/ipc/

SysV things too
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
source code for read() function ? Mike Davies Linux - Software 1 11-03-2004 02:10 PM
time taken to read the information present in pipe ujnam Programming 0 10-27-2004 04:19 AM
Read the output from a pipe with bash ? fluppi Linux - Software 3 01-13-2004 12:59 PM
Read() function In "socket.h" RAW_STREAM Penguinizer Programming 3 02-22-2003 01:14 AM
file function in linux: read() minh2003 Programming 1 10-08-2002 01:23 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration