Piping and C

Hi. I am new in shell programming. I have this problem which I want to teach myself. Please head me in the right direction:

"Your program should read the output of wc as standard input. In another words, the program is to behave as if it were part of the pipeline. Typical execution would be

wc demofile.txt | your_program -[b | w | l]

Example 1:
$ wc demofile.txt | ./program -b
1) The number of files : 1
2) The largest file in byte : demofile.txt (7148 bytes)
3) The smallest file in byte : demofile.txt (7148 bytes) 4) The total number of bytes : 7148 bytes

How can I do this...I am complete noob in shell programming...

To start how I can I read the text file as string in to c++ code? What should be the var? what is |

Thanks
Are you studying shell programming or "C"?
koothkeeper: read the post ;)

well first you need to understand what piping means in a shell. http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-4.html

then, once you understand how piping basically works, you just need to read from stdin to get the output, and your program will be able to work with what you need.
Your program should read standard input.

That.

Run the command
wc demofile.txt

Run the wc with different textfiles. Observe what is the output of the program wc.

Now write your program and make it read from standard input (std::cin) text that looks like the output of program wc.


PS. You don't state how the command line parameters -b, -w, -l should affect your program.
Last edited on
Here I did the coding:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>

using namespace std;

int main(int argc, char* argv[]){

    int size_nm=0;

    if(argc==1){
        cout<<"insufficient arguments entered"<<endl;
        return 0;}

    for(int i=1;i<argc;i++){
        if(*argv[i]=='b' || *argv[i]=='w' || *argv[i]=='l'){
            cin>>size_nm;
            cout<<size_nm<<endl;}
        else if(*argv[i]=='b' && *argv[i+1]=='w' && *argv[i+2]=='l'){
            cin>>size_nm;
            cout<<size_nm<<endl;
            }

        else
            cout<<"invalid command"<<endl;
        }



    return 0;
}


Output:

ashari@ashari-VirtualBox:~/Desktop/CPP/tests$ wc salam.txt | ./pipe b
1
ashari@ashari-VirtualBox:~/Desktop/CPP/tests$ wc salam.txt | ./pipe b w
1
1
ashari@ashari-VirtualBox:~/Desktop/CPP/tests$ wc salam.txt | ./pipe b w l
1
1
6
ashari@ashari-VirtualBox:~/Desktop/CPP/tests$ wc salam.txt | ./pipe b w l k
1
1
6
invalid command
ashari@ashari-VirtualBox:~/Desktop/CPP/tests$


Please tell me how as I increase command line argument for the pipe program output also comes correctly?? Also how can I output the file name? I tried adding get line, but it does not print out filename in cmd line
Last edited on
Know the input data.
# wc bar.txt
16 13 69 bar.txt

# wc bar.txt main.txt
 16  13  69 bar.txt
 20  40 309 main.txt
 36  53 378 total

The 'wc' with one filename prints out one line of output that contains: number number number filename.
The 'wc' with more than one filename prints out one line for each file and then a total line.

Do man wc and read carefully. Then you should know what your input data should contain.

Your program should read one or more lines from std::cin. Your current program reads at most one integer.


I do interpret the synopsis of your program as the program expecting at most one command line argument. One word that should be "-b", "-w" or "-l".

You have not told whether your homework assignment instructions describe what those arguments mean. It is surprising, if they don't.
I will try reading as a line,

-b bytes -w words -l lines

thanks..let me try
-b bytes -w words -l lines

In other words, the argument tells which number of the three you are interested in.
Topic archived. No new replies allowed.