Thread: help me with piping...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    5

    Question help me with piping...

    I've started to learn c programing 2 weeks ago (i have gone to class tree times) and our instructor gave us a homework...
    and I have to submit it for the next week ...
    I have to mention that the biggest program I have written with c is: " hello world!!!"
    he said that copy the content of a.txt to b.txt by using pipes...and I don't know any thing about piping yet and i didn't find any thing useful on the web,and I want to know how should I use piping in windows?!!...

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    5

    Talking

    by the way excuse my english...

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    58
    Piping after only two weeks of class? I find it hard to believe that a more advanced subject is being given so early in a course. Are you sure your instructor said to use piping in a 'C' program? You can use piping from the command line, which is probably what the instructor meant. There is something missing from your story that you haven't told us. Is this an 'Intro to C' course or a more advanced course?

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    5
    Quote Originally Posted by samf View Post
    Piping after only two weeks of class? I find it hard to believe that a more advanced subject is being given so early in a course. Are you sure your instructor said to use piping in a 'C' program? You can use piping from the command line, which is probably what the instructor meant. There is something missing from your story that you haven't told us. Is this an 'Intro to C' course or a more advanced course?
    last term I passed python and this term we're going to master c...

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    58
    It doesn't matter what you took previously. A first class in a new language does not usually begin with piping until you have mastered other basic concepts like conditions, looping, etc.

    If you are willing to tackle this more advanced topic, try this...

    Cprogramming.com FAQ > Working with Processes

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    5

    Unhappy

    thanx , but it's a little bit confusing, I just want to copy a text file to another by editing the following code and also using pipes:
    Code:
    int c;
    while((c=getchar())!=EOF){
        putchar(c);
    }

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    5

    Unhappy

    Quote Originally Posted by samf View Post
    It doesn't matter what you took previously. A first class in a new language does not usually begin with piping until you have mastered other basic concepts like conditions, looping, etc.

    If you are willing to tackle this more advanced topic, try this...

    Cprogramming.com FAQ > Working with Processes
    thanx , but it's a little bit confusing, I just want to copy a text file to another by editing the following code and also using pipes:

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Pipes are not part of standard C, so I don't see why an introductory course to C should even mention them.

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    58
    Does your instructor mean for you to create a program that would take a file and send the output to stdout? Most programs that can accept stdin data can have its data piped into it via shell commands like so:

    Code:
    $ cat file.dat
    Apple 1.1
    Apple 1.5
    Apple 1.9
    Banana 1.3
    Banana 2.7
    Banana 3.3
    
    $ cat file.dat | grep Banana
    Banana 1.3
    Banana 2.7
    Banana 3.3
    The '|' is the piping symbol that takes stdout from the previous program and funnels it into the stdin in of the program on the right side of the '|' symbol.

    If this is similar to what you want, all you have to do is use putchar to write the contents of the file to stdout, which it seems you are already doing. You now have to use getchar to read the file and use putchar to send it to stdout.

  10. #10
    Registered User
    Join Date
    Feb 2010
    Posts
    1

    Talking Hi there

    seems that there are some classmates of mine over here...

  11. #11
    Registered User
    Join Date
    Nov 2008
    Location
    INDIA
    Posts
    64

    Thumbs up Is this ok

    Ya,it is a advanced concept.But easy to understand.

    Pipe is half duplex.In c we can achieve the pipe by using the function pipe().It internally uses fork.I think you may aware of fork.It will create a new process.

    Pipe() will return two descriptors.one descriptor fd[0] is for reading and another descriptor fd[1] for writing.So if you write in the write end and you can read that in another end.For your requirement you just read from the source file and write that data it in the fd[1] (write end) then read that in the read end then you just write the data in the destination file.

    To know in detail refer : man pipe .

    I tried the requirement

    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include<fcntl.h>
    #include<stdlib.h>
    
    #define MAXSIZE 1024
    main()
    {
            int     fd[2];
            pid_t   childpid;
            int f_desc,f_desc1,n;
            char buf[MAXSIZE];
    
            pipe(fd);
    
            if((childpid = fork()) == -1)
            {
                    perror("fork");
                    exit(1);
            }
    
            if(childpid == 0)
            {
                    close(fd[1]);
                    if((n=read(fd[0],buf,MAXSIZE))!=-1)
                    {
                            if((f_desc1=open("backup",O_CREAT|O_RDWR,0644))!=1)
                            {
                                    write(f_desc1,buf,n);
                            }
                            else
                            {
                                    printf("open : error \n");
                            }
                    }
                            else
                            {
                                    printf("read error");
                            }
                    }
                    else
                    {
                            close(fd[0]);
                            if((f_desc=open("a.c",O_RDONLY))!=-1)
                            {
                                    if((n=read(f_desc,buf,MAXSIZE))!=-1)
                                    {
                                            write(fd[1],buf,n);
                                    }
                                    else
                                    {
                                            printf("read error");
                                    }
                            }
                            else
                            {
                                    printf("open : error \n");
                            }
                    }
            }
    Last edited by karthigayan; 02-23-2010 at 12:27 AM. Reason: To change you to the

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. piping a file into my project
    By Tom_Arch in forum C Programming
    Replies: 1
    Last Post: 04-27-2009, 12:41 AM
  2. Advanced Piping Question!
    By Paul22000 in forum C Programming
    Replies: 14
    Last Post: 04-17-2009, 09:55 AM
  3. prevent piping?
    By barneygumble742 in forum C++ Programming
    Replies: 2
    Last Post: 12-29-2005, 01:07 AM
  4. Piping error messages to a file?
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 02-25-2002, 03:09 PM

Tags for this Thread